C语言怎么把一个文件转换为数组
1个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
struct stuInfo{
char name[20];
int num;
int a,b,c,d;
};
int main(){
FILE *fp;
char fileName[]="a.txt";
int n,i;
struct stuInfo stu[1000];
if((fp=fopen(fileName,"r"))==NULL){
printf("Error!\n");
exit(0);
}
fscanf(fp,"%d",&n);
for(i=0;i<n;i++){
fscanf(fp,"%d%s%d%d%d%d",&stu[i].num,
&stu[i].name,
&stu[i].a,
&stu[i].b,
&stu[i].c,
&stu[i].d
);
printf("%d %s %d %d %d %d\n",stu[i].num,
stu[i].name,
stu[i].a,
stu[i].b,
stu[i].c,
stu[i].d
);
}
fclose(fp);
return 0;
#include<stdlib.h>
struct stuInfo{
char name[20];
int num;
int a,b,c,d;
};
int main(){
FILE *fp;
char fileName[]="a.txt";
int n,i;
struct stuInfo stu[1000];
if((fp=fopen(fileName,"r"))==NULL){
printf("Error!\n");
exit(0);
}
fscanf(fp,"%d",&n);
for(i=0;i<n;i++){
fscanf(fp,"%d%s%d%d%d%d",&stu[i].num,
&stu[i].name,
&stu[i].a,
&stu[i].b,
&stu[i].c,
&stu[i].d
);
printf("%d %s %d %d %d %d\n",stu[i].num,
stu[i].name,
stu[i].a,
stu[i].b,
stu[i].c,
stu[i].d
);
}
fclose(fp);
return 0;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询