gpt4 book ai didi

c - 从文件中读取数据,c

转载 作者:太空宇宙 更新时间:2023-11-04 04:19:19 24 4
gpt4 key购买 nike

我在c中练习文件的主题,我目前正在学习基础知识,这意味着我不控制语言内置的所有功能,以及一些命令,所以如果你能根据基本命令解释什么复杂的我会感谢你。

程序的作用:吸收学生资料(身份证、姓名、电话),并打印每个学生的资料在文件中(每个学生资料会另起一行)

我的代码有什么问题:我写了一个打印文件中所有数据的函数,如果用户插入了多个学生,该函数只打印文件中的第一行,仅此而已。

当我使用 F10 进入循环时,第一次运行打印出第一个学生的详细信息,然后第二次运行时,fscanf 命令返回一个值 (-1) 并且没有打印出第一个学生的详细信息第二个学生。

我很乐意指导和纠正。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct studnt
{
int id;
int number;
char *name;
}typedef s_studnt;
void print_studnt(FILE *q,char name_file[])
{
int id, number;
char str_name[20];
q=fopen(name_file, "r");
if (q == NULL)
{
printf("eror fac\n");
return;
}
printf("\n");
while (!feof(q))
{

fscanf(q, "%d %s %d", &id, str_name, &number); //at the next loop, fscanf return -1
printf("studnt name:%s\nthe id studnt:%d\ntelephon number:%d\n", str_name, id, number);
printf("\n");
}
fclose(q);
return;
}
int main()
{
int size, i, length;
char str[20], name_file[20];
FILE *q = NULL;
s_studnt *studnt = NULL;
printf("how much studnt you have:\n");
scanf("%d", &size);
studnt = (s_studnt*)malloc(size * sizeof(s_studnt));
if (studnt == NULL)
{
printf("eror 1\n");
return 0;
}
printf("enter file name:\n");
scanf("%s", name_file);
q = fopen(name_file, "w");
if (q == NULL)
{
printf("eror file\n");
return 0;
}
fclose(q);
for (i = 0; i < size; i++)
{
printf("enter studnt name:\n");
scanf("%s", str);
length = strlen(str);
studnt[i].name = (char*)malloc(length+1 * sizeof(char));
if (studnt[i].name == NULL)
{
printf("eror2\n");
return 0;
}
strcpy(studnt[i].name, str);
printf("enter the id studnt:\n");
scanf("%d", &studnt[i].id);
printf("enter your telephon number:\n");
scanf("%d", &studnt[i].number);
q = fopen(name_file, "a");
if (q == NULL)
{
printf("eror write\n");
return 0;
}
fprintf(q, "%d %s %d\n", studnt[i].id, studnt[i].name, studnt[i].number);

}
fclose(q);
print_studnt(q,name_file);
return 0;
}

最佳答案

以下代码有效。您必须注意不要太频繁地打开和关闭文件,即在循环内:打开文件,执行循环并在循环后关闭文件。

然后 fscanf() 出现了问题。 fscanf() 会一直读取到下一个空格,所以当您以 fscanf() "%d %s %d" 的格式指定时读取到第三个空格然后停止。您可以通过下面代码中的方法解决此问题(读取 fscanf() 的返回值),因为如果 fscanf() 返回 EOF使用 fscanf() 下面的代码读取它 ( http://www.cplusplus.com/reference/cstdio/fscanf/ ) 继续逐行扫描文件,直到它读取 EOF。使用 fscanf() 逐行读取文件不被认为是好的做法 (reading lines using fscanf)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct studnt
{
int id;
int number;
char *name;
}typedef s_studnt;

void print_studnt(FILE *q,char name_file[])
{
int id, number;
char str_name[20];

q=fopen(name_file, "r");
if (q == NULL)
{
printf("eror fac\n");
return;
}
printf("\n");

int r;
r=0;

do
{
r = fscanf(q, "%d %s %d", &id, str_name, &number);
if(r==EOF) break;
printf("studnt name:%s\nthe id studnt:%d\ntelephon number:%d\n", str_name, id, number);
printf("\n");

} while (r != EOF);
fclose(q);
return;
}


int main()
{
int size, i, length;
char str[20], name_file[20];
FILE *q = NULL;
s_studnt *studnt = NULL;
printf("how much studnt you have:\n");
scanf("%d", &size);
studnt = (s_studnt*)malloc(size * sizeof(s_studnt));
if (studnt == NULL)
{
printf("eror 1\n");
return 0;
}
printf("enter file name:\n");
scanf("%s", name_file);
q = fopen(name_file, "w");
if (q == NULL)
{
printf("eror file\n");
return 0;
}


for (i = 0; i < size; i++)
{

printf("enter studnt name:\n");
scanf("%s", str);
length = strlen(str);
studnt[i].name = (char*)malloc(length+1 * sizeof(char));
if (studnt[i].name == NULL)
{
printf("eror2\n");
return 0;
}
strcpy(studnt[i].name, str);
printf("enter the id studnt:\n");
scanf("%d", &studnt[i].id);
printf("enter your telephon number:\n");
scanf("%d", &studnt[i].number);

fprintf(q, "%d %s %d\n", studnt[i].id, studnt[i].name, studnt[i].number);

}
fclose(q);
print_studnt(q,name_file);
return 0;
}

关于c - 从文件中读取数据,c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48318799/

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