编程实现功能:可将整个students结构体数组存到硬盘上,并从存好的硬盘上读到内存的students结构体数组中


#include <stdio.h>
#define NC 100
struct studentRed {  //定义学生记录,即对应表格的一行
    char studName[20]; //姓名对应第一列的Student Name
    char studID[20]; //学号对应第二列的Student ID 用20个字符编码,可用字母做学号
    float compProgram; // score for Computer programming 用浮点数可输入小数分数
    float physEducat;  //score for Computer programming
    float commResech;  //score for Communication and Research
    float averageScore;  //average score
}*p,students[NC]={
        {"John","zy001",84,86,78,0},
        {"Xiaoming","zy002",77,82,90,0},
        {"Xiaohua","zy003",79,82,85,0},
        {"Jianguo","zy004",84,86,78,0},
        {"Xiaodong","zy005",60,55,40,0},
};

void insertAvgScore(struct studentRed *stu,int length){
    for (int i = 0; i <length ;++i) {
        stu[i].averageScore=(stu[i].compProgram+stu[i].physEducat+stu[i].commResech)/3;
    }
}
void printStudentInfo(struct studentRed *stu,int length){
    printf("Name\t\tID\t\tComputer\tPhysical\tCommunication\tAverages\n");
    for(int i=0;i<length;i++){
        printf("%-12s%s\t%f\t%f\t%f\t\t%f\n",stu[i].studName,stu[i].studID,stu[i].compProgram,stu[i].physEducat,stu[i].commResech,stu[i].averageScore);
    }

}

int main(){
    insertAvgScore(students,5);
    printStudentInfo(students,5);
    return 0;
}

修改了一下,如下:

#include <stdio.h>

#define NC 100
struct studentRed {  //定义学生记录,即对应表格的一行
    char studName[20]; //姓名对应第一列的Student Name
    char studID[20]; //学号对应第二列的Student ID 用20个字符编码,可用字母做学号
    float compProgram; // score for Computer programming 用浮点数可输入小数分数
    float physEducat;  //score for Computer programming
    float commResech;  //score for Communication and Research
    float averageScore;  //average score
}*p,students[NC]={
        {"John","zy001",84,86,78,0},
        {"Xiaoming","zy002",77,82,90,0},
        {"Xiaohua","zy003",79,82,85,0},
        {"Jianguo","zy004",84,86,78,0},
        {"Xiaodong","zy005",60,55,40,0},
};
 
void insertAvgScore(struct studentRed *stu,int length){
    for (int i = 0; i <length ;++i) {
        stu[i].averageScore=(stu[i].compProgram+stu[i].physEducat+stu[i].commResech)/3;
    }
}
void printStudentInfo(struct studentRed *stu,int length){
    printf("Name\t\tID\t\tComputer\tPhysical\tCommunication\tAverages\n");
    for(int i=0;i<length;i++){
        printf("%-12s%s\t%f\t%f\t%f\t\t%f\n",stu[i].studName,stu[i].studID,stu[i].compProgram,stu[i].physEducat,stu[i].commResech,stu[i].averageScore);
    }
 
}
 
int main(){
//    printf("0\n");
    insertAvgScore(students,5);
    //printStudentInfo(students,5);
    
    FILE * fp = fopen("f:\\stu.txt","w+");
    if(fp==NULL){
        printf("打开文件失败或创建文件失败,程序退出!\n");
        return 0; 
    }
    
    int i;
    //存放文件 
    for(i=0;i<5;i++){
        fprintf(fp,"%s %s %f %f %f %f ",students[i].studName,students[i].studID,students[i].compProgram,students[i].physEducat,students[i].commResech,students[i].averageScore); 
    } 
     
    fclose(fp);
    
    fp = fopen("f:\\stu.txt","r");
    if(fp==NULL){
        printf("打开文件失败,程序退出!\n");
        return 0; 
    }
    
    struct studentRed t[NC] ;
//    printf("1\n"); 
    printf("Name\t\tID\t\tComputer\tPhysical\tCommunication\tAverages\n");
    //从文件读取并打印 
    for(i=0;i<5;i++){
//        fscanf(fp,"%s",t.studName);
//        fscanf(fp,"%s",t.studID);
//        fscanf(fp,"%f",t.compProgram);
//        fscanf(fp,"%f",t.physEducat);
//        fscanf(fp,"%f",t.commResech);
//        fscanf(fp,"%f",t.averageScore);
    //    printf("1.1\n");
        fscanf(fp,"%s%s%f%f%f%f ",t[i].studName,t[i].studID,&t[i].compProgram,&t[i].physEducat,&t[i].commResech,&t[i].averageScore); 
    //    printf("\n21\n");
        printf("%-12s%s\t%f\t%f\t%f\t\t%f\n",t[i].studName,t[i].studID,t[i].compProgram,t[i].physEducat,t[i].commResech,t[i].averageScore);    
        //printf("\n3\n");
    } 
      
    fclose(fp); 
    return 0;
}