怎样用C语言从txt文件中读入数据?

我C盘根目录下有一个stud_record的txt文件,现在程序中要把这个文件中的数据逐个读入到已定义的数组中,请问应该怎么做?我直接用fread读入并让结果输出到屏幕,但运行的时候没有反应,为什么?
谢谢!
#include <stdio.h>
struct student
{
char stu_num[5];
char name[10];
int age;
int score;
}stu[9];
print_rec(char a[],char b[],int c,float d);
main()
{
i=0;
FILE *fp;
if((fp=fopen("C:\\stud_r~1.txt","r"))==NULL)
{printf("Cannot open the file\n");exit(0);}
for(i=0;i<9;i++)
fread(&stu[i],sizeof(struct student),1,fp);
print_rec(stu[i].stu_num,stu[i].name,stu[i].age,stu[i].score);
getch();
}

print_rec(char a[],char b[],int c,float d)
{
int j;
for(j=0;j<9;j++)
printf("%7s%10s%4d%6f\n",a[j],b[j],c,d);
}
这是我要弄得程序的一部分,所以那个输出的函数要作为子函数调用,大家能帮忙看看哪儿有问题吗?
谢谢!

1 以fopen打开文件,使用"r"方式。

2 通过fscanf,按照文件中的数据格式,读入数据。

3 关闭文件并使用数据。

如文件in.txt中存在三个以空格分隔的数据,依次为整型,字符串,以及浮点型,则读取数据的代码可以写作:

int main()
{
    FILE *fp;
    int a;
    char s[100];
    float f;
    fp = fopen("in.txt", "r");
    if(fp == NULL) return -1;//打开文件失败,结束程序。
    fscanf(fp,"%d%s%f",&a,s,&f);
    fclose(fp);
    
    printf("read value: %d, %s, %f", a,s,f);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-11-25
//其中的in.txt就是你要读取数据的文件,当然把它和程序放在同一目录
-------------------------------------
#include
<stdio.h>
int
main()
{
int
data;
FILE
*fp=fopen("in.txt","r");
if(!fp)
{
printf("can't
open
file\n");
return
-1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return
0;
}
第2个回答  2020-02-08
#include
<stdio.h>
int
main()
{
FILE
*fp=NULL;
int
a[160];
int
i=0;
fp=fopen("data.txt","r");
if
(
!fp
)
{
printf("open
file
error\n");
return
-1;
}
while(
!feof(fp)
)
{
if
(
fscanf(
fp
,
"%d"
,&a[i]
)
!=1
)
break
;
i++;
fgetc(fp)
;//过滤掉分隔符
}
fclose(fp);
//以下倒序输出数据
printf("i=%d\n"
,
i
);
while(
--i
>=
0
)
{
printf("%d,"
,
a[i]
);
if
(
i
%10
==
0
)
printf("\n")
;
}
return
0;
}
第3个回答  2019-05-03
你这是ascii格式的文件,最适合用fscanf来读,例如:
fscanf(fp,
"%s
%s
%d
%f",stu[i].stu_num,stu[i].name,&stu[i].age,&stu[i].score);
fread适用于读定长记录,你这样用当然要出错。
唉,我这只是举例说明,看来你对c还要深入了解。凡是你希望将变量作为参数传递给某函数,并希望在该函数内部改变变量的值,你都需要传递变量的地址,即使用&符号。你在自己的fread里面不是也使用了&stu[i]吗?但是由于字符数组自身已经是地址,所以stu[i].stu_num之类的东西就不要再加&了,否则反而出错。
我并没有你的数据文件,仅从你的帖子上看,你在fscanf的格式字符串最后再加一个回车字符,应该能跳过行尾。建议你再试试
fscanf(fp,
"%s
%s
%d
%f\n",stu[i].stu_num,stu[i].name,&stu[i].age,&stu[i].score);
第4个回答  2007-03-23
这要看你的代码是什么样的。
贴出我的验证代码你看看有什么不同。

有一点要注意如果你用的是TC之类的编译器,它是DOS下工作的不支持8个字符以上的长文件名,所以要用短名如:stud_r~1.txt
#include "stdio.h"
void main()
{

FILE *pf;
char str[100];
int i;
for(i=0;i<100;i++) str[i]=0;
pf=fopen("C:\\record.txt","r");
fread(str,100,100,pf);
fclose(pf);
printf("\n%s",str);
}

看了你的代码,有几处不妥修正如下:
#include <stdio.h>
struct student
{
char stu_num[5];
char name[10];
int age;
int score;
}stu[9];
print_rec(struct student *stud); /*你的访问方法行不通,要这样*/
main()
{
i=0;
FILE *fp;
fp=fopen("C:\\stud_r~1.txt","r");/*这里一定不能放在if里面,这个赋值返回是恒为真的。*/

if(fp==NULL)
{printf("Cannot open the file\n");exit(0);}
for(i=0;i<9;i++)
fread(&stu[i],sizeof(struct student),sizeof(struct student),fp);/*第三个参数是读取的字节数所以不能是1*/
print_rec(stu);
fclose(fp);/*事情做完记得收拾工具*/
getch();
}

print_rec(struct student *stud)
{
int j;
for(j=0;j<9;j++)
printf("%7s%10s%4d%6f\n",stud[j].stu_num,stud[j].name,stud[j].age,stud[j].score);
}本回答被提问者采纳
相似回答