复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <stdio.h> #include <string.h> #include <stdlib.h> struct infostu { char no[20]; //学号 char name[20]; char sex[4]; int age; char major[20]; //专业班级 }; int main() { int i=0,j; struct infostu student[500]; FILE *fp; if((fp=fopen("c:\student.txt","r"))==NULL) // 打开文件并且让fp指向 文件 { printf("Can not open filen"); exit(1); } while(!feof(fp)) //读取一组数据后指针指向下一组数据,并且判断是否指向最后一行 { fscanf(fp,"%[^,],%[^,],%[^,],%d,%s",&student[i].no,&student[i].name,&student[i].sex,&student[i].age,&student[i].major); i++; } //%[^,] 字符串以,为结束标志 j=i; for(i=0;i<j;i++) printf("%s,%s,%s,%d,%sn",student[i].no,student[i].name,student[i].sex,student[i].age,student[i].major); fclose(fp); //关闭文件; return 0; }
最后
以上就是完美帅哥最近收集整理的关于C语言读取文件数据到结构体数组的全部内容,更多相关C语言读取文件数据到结构体数组内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复