70,020
社区成员




void read_data(void)
{
FILE *fp;
int i,j;
char str[1000];
fp = fopen("th.txt","r");
if (fp == NULL)
{
printf("Can't open the file!\n");
return ;
}
fgets(str,1000,fp);//此句中str从fp中读取数据后,下面语句中并没有用到str,
// str的作用的什么?此句为何不能少?
for (i=0; i<N; i++)
{
fscanf(fp,"%s\t%d\t",stud[i].name,&stud[i].num);
for (j=0; j<M; j++)
{
fscanf(fp,"%d\t",&stud[i].score[j]);
}
}
printf("姓名 学号 英语 C语言 线性代数 离散数学 高等数学 密码学\n");
for (i=0; i<N; i++)
{
printf("%s\t%d\t",stud[i].name,stud[i].num);
for (j=0; j<M; j++)
{
printf("%d\t",stud[i].score[j]);
}
printf("\n");
}
}
学号 姓名 英语 C语言 线性代数 离散数学 高等数学 密码学
1 花小霜 90 92 93 91 89 78
2 柳莺莺 91 85 59 88 65 92
3 公羊羽 89 68 91 89 87 88
4 项少龙 88 77 66 55 87 94
5 董帅民 89 92 77 91 65 67
6 高进 88 56 88 78 69 89
7 花清啸 89 65 82 94 84 89
8 王小宝 69 58 44 24 33 55
9 谷慎 92 91 90 93 94 91
10 陆见 85 89 84 81 83 92
文件内容。。
#define M 10 //学生数
#define N 6 //课程数
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct student
{
int num;
char name[20];
int score[N];
float ave;
}st[M];
void read_dat1()
{
int i,j;
char str1[100];
FILE *fp;
fp=fopen("th.txt","r");
if(fp==NULL)
{
printf("cannot open");
exit(0);
}
fgets(str1,10000,fp);
for(i=0;i<M;i++)
{
fscanf(fp,"%d%s",&st[i].num,st[i].name);
for(j=0;j<N;j++)
{
fscanf(fp,"%d",&st[i].score[j]);
}
}
printf("学号 姓名 英语 C语言 线性代数 离散数学 高等数学 密码学\n");
for(i=0;i<M;i++)
{
printf("%d\t%s\t",st[i].num,st[i].name);
for(j=0;j<N;j++)
printf("%d\t",st[i].score[j]);
printf("\n");
}
fclose(fp);
}
void main()
{
read_dat1();
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define N 10 //学生数
#define M 6 //课程数
struct student
{
int num;
char name[20];
int score[M];
}stud[N];