gpt4 book ai didi

c - C语言读取函数结构体和txt文件

转载 作者:行者123 更新时间:2023-11-30 20:19:23 25 4
gpt4 key购买 nike

有人可以帮我编写一个从 .txt 文件读取的函数吗?我做了所有这些,并且编写了很多不同的函数来从文件中读取,但它们都不起作用。每次我按下选项 2 时,它都不会显示我写入文件的内容。代码工作没有错误,我只是不知道如何编写阅读功能。

标题.h

#ifndef HEADER_H
#define HEADER_H


typedef struct tenant {
char fname[30];
char lname[30];
int floor;
int phone;
}TENANT;



void write(FILE*, int, TENANT*);


#endif

来源.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "Header.h"
#include <iostream>
#include <conio.h>


int main() {

int n = 0, helper = 0;
char nameFile[100];
TENANT *tenant = NULL;
tenant = (TENANT*)calloc(200, sizeof(TENANT));
FILE *file = NULL;


printf("Name of building: ");
scanf("%s", nameFile);
printf("\n");

while (n != 4) {
printf("Press 1 for creating file! \n");
printf("Press 2 for reading from file! \n");
printf("Press 3 for adding new tenants! \n");
printf("Press 4 to close the file! \n\n");
printf("Number: ");
scanf("%d", &n);
printf("\n");

switch (n)
{
case 1:
file = fopen(nameFile, "w");
printf("File created.\n");
printf("\n");
fclose(file);
break;

case 2:
if ((file = fopen(nameFile, "r")) == NULL)
{
printf("File is not yet created!\n\n");
break;
}
else
{
file = fopen(nameFile, "r");
read(file, helper, tenant);
printf("\n");
fclose(file);
}
break;

case 3:
helper++;
printf("Insert details of tenant: \n", helper);
file = fopen(nameFile, "a");
write(file, helper, tenant);
printf("\n");
fclose(file);
break;

case 4:
printf("Program closed!\n");
break;

default:
break;

}
}
free(tenant);
system("PAUSE");
return 0;
}

函数.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "Header.h"

void write(FILE* file, int helper, TENANT* tenant) {

int i = helper - 1;
printf("Name of tenant: ");
scanf("%s", (tenant + i)->fname);

printf("Laste name of tenant: ");
scanf("%s", (tenant + i)->lname);

printf("Floor: ");
scanf("%d", &(tenant + i)->floor);

printf("Phone: ");
scanf("%d", &(tenant + i)->phone);

fprintf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
fprintf(file, "\n//////////////////////////////////////////////////////////////////////////\n");

}

void read(FILE* file, int helper, TENANT* tenant)
{

fread(tenant, sizeof(*tenant), 1, file);
for (int i = 0; i < helper; i++)
{
fscanf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
}
}

最佳答案

我认为你不应该将你的函数命名为 write(),因为有一个名为 write() 的系统调用,请参阅 man 2 write。阅读同样如此。

有一些错误你应该注意,也许用 -Wall 编译你的代码,这样你就可以看到。printf() 函数的格式中没有 %d,但您向它传递了变量助手。

printf("Insert details of tenant: \n", helper);

在你的 read() 函数中,你不需要这行代码

fread(tenant, sizeof(*tenant), 1, file);

这有很多问题。你应该阅读 man 3 fread 来看看它是如何工作的。

所以,只需让你的阅读功能像这样

void read(FILE* file, int helper, TENANT* tenant)
{
for (int i = 0; i < helper; i++)
{
fscanf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
}
}

在你的主程序中,我认为你会遇到变量助手的问题。您在主函数中初始化了它,如下所示

int helper=0;

因此,第一次调用 write 函数时,您将传递值为 0 的变量助手,并且在函数内部,您的索引器将从 -1 开始。这将导致索引超出范围和未定义的程序行为。也许你应该从一开始就将 helper 初始化为 1。每次调用读取或写入函数后都会增加助手,这样您就不会覆盖租户[0]。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


typedef struct tenant {
char fname[30];
char lname[30];
int floor;
int phone;
}TENANT;


void write(FILE* file, int helper, TENANT* tenant) {

int i = helper - 1;
printf("Name of tenant: ");
scanf("%s", (tenant + i)->fname);

printf("Laste name of tenant: ");
scanf("%s", (tenant + i)->lname);

printf("Floor: ");
scanf("%d", &(tenant + i)->floor);

printf("Phone: ");
scanf("%d", &(tenant + i)->phone);

fprintf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, (tenant + i)->floor, (tenant + i)->phone);
fprintf(file, "\n//////////////////////////////////////////////////////////////////////////\n");

}

void read(FILE* file, int helper, TENANT* tenant)
{

for (int i = 0; i < helper; i++)
{
fscanf(file, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", (tenant + i)->fname, (tenant + i)->lname, &((tenant + i)->floor), &((tenant + i)->phone));
}
}


int main() {

int n = 0, helper = 1;
char nameFile[100];
TENANT *tenant = NULL;
tenant = (TENANT*)calloc(200, sizeof(TENANT));

FILE *file = NULL;


printf("Name of building: ");
scanf("%s", nameFile);
printf("\n");

while (n != 4) {
printf("Press 1 for creating file! \n");
printf("Press 2 for reading from file! \n");
printf("Press 3 for adding new tenants! \n");
printf("Press 4 to close the file! \n\n");
printf("Number: ");
scanf("%d", &n);
printf("\n");

switch (n)
{
case 1:
file = fopen(nameFile, "w");
printf("File created.\n");
printf("\n");
fclose(file);
break;

case 2:
if ((file = fopen(nameFile, "r")) == NULL)
{
printf("File is not yet created!\n\n");
break;
}
else
{
printf("read");
read(file, helper++, tenant);

//after you test your program for reading
//just delete fprintf :)
fprintf(stdout, "Name: %s\nLast name: %s\nFloor: %d\nPhone: 0%d\n", tenant[0].fname, tenant[0].lname, tenant[0].floor, tenant[0].phone);
printf("\n");
fclose(file);
}
break;

case 3:
helper++;
printf("Insert details of tenant: \n");
file = fopen(nameFile, "a");
write(file, helper, tenant);
printf("\n");
fclose(file);
break;

case 4:
printf("Program closed!\n");
break;

default:
break;

}
}
free(tenant);
system("PAUSE");
return 0;
}

请注意,您正在使用变量助手进行读取和写入。也许为此使用不同的变量,因为如果您在写入时增加变量助手,您将不会从文件的开头读取:)

关于c - C语言读取函数结构体和txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50295541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com