C语言文件读取之后什么也没有
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct student{
char name;
char major;
int Eng_sco;
int cal_sco;
int c_sco;
int sum;
};
int tx_per=0,tx_ave,tx_sum=0;
int wl_per=0,wl_ave,wl_sum=0;
int wlw_per=0,wlw_ave,wlw_sum=0;
int ReadStudentInfo(const char* filename, struct student** pStudents);
void Count(struct student pStudents[], int num);
int main()
{
int count;
int m;
struct student *list;
count=ReadStudentInfo("readme.txt", &list);
printf("学生信息: \n");
printf("----------------------------------------------------\n");
printf("序号姓 名专业名称 英语成绩 线性代数成绩 c语言成绩 总成绩\n");
for(m=1;m<=count;m++)
{
printf("%4d %s %s %4d %4d %4d %4d\n",m,(list+m-1)->name,(list+m-1)->major,(list+m-1)->Eng_sco,(list+m-1)->cal_sco,(list+m-1)->c_sco,(list+m-1)->sum);
}
Count(list, count);
printf("\n统计信息 :\n");
printf("----------------------------------------------------\n");
printf("序号专业名称 学生人数 平均成绩\n");
printf(" 1 通信 %d %d\n",tx_per,tx_ave);
printf(" 2 网络 %d %d\n",wl_per,wl_ave);
printf(" 3 物联网 %d %d\n",wlw_per,wlw_ave);
return 0;
}
int ReadStudentInfo(const char* filename, struct student** pStudents)
{
FILE *fp;
char ch;
int i=1,j;
fp=fopen("data.txt","r");
if(fp==NULL)
{
printf("cann't open the file");
exit(-1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch=='\n')
{
i++;
}
ch=fgetc(fp);
}
rewind(fp);
*pStudents=(struct student*)malloc(sizeof(** pStudents)*i);
for(j=0;j<i;j++)
{
fread(&*pStudents,sizeof(struct student)-sizeof(int),1,fp);
(*pStudents+j)->sum=((*pStudents+j)->Eng_sco+(*pStudents+j)->cal_sco+(*pStudents+j)->c_sco);
}
fclose(fp);
return i;
}
void Count(struct student pStudents[], int num)
{
int n=1;
for(n=1;n<=num;n++)
{
if(!strcmp((pStudents+n-1)->major,"通信"))
{
tx_per++;
tx_sum=tx_sum+(pStudents+n-1)->sum;
}
if(!strcmp((pStudents+n-1)->major,"网络"))
{
wl_per++;
wl_sum=wl_sum+(pStudents+n-1)->sum;
}
if(!strcmp((pStudents+n-1)->major,"物联网"))
{
wlw_per++;
wlw_sum=wlw_sum+(pStudents+n-1)->sum;
}
tx_ave=tx_sum/tx_per;
wl_ave=wl_sum/wl_per;
wlw_ave=wlw_sum/wlw_per;
}
free(pStudents);
}
int ReadStudentInfo(const char* filename, struct student** pStudents)
{
FILE *fp;
char ch;
int i=1,j;
fp=fopen(filename,"r");
页:
[1]