#include <stdio.h>#include<stdlib.h>///define student nodes.typedefstructnode{Charname[ -]; floatCscore; floatDatabasescore;} Student;//Keyboard input Student Informationstudent* Scanstudent (intN) {Student*studentarr= (student*)malloc(sizeof(Student) *N); for(intI=0; i<n;i++) {printf ("Please enter the name of%d student, C language score, database score:", (i+1)); //studentarr[i].name= (char*) malloc (sizeof (char) *20);scanf"%s%f%f", &studentarr[i].name,&studentarr[i]. cscore,&Studentarr[i]. Databasescore); } returnStudentarr;}//read n Student information from a filestudent* GetFile (Char*filepath,intN) {FILE*fp=fopen (FilePath,"R"); Student*studentarr=NewStudent[n]; for(intI=0; i<n;i++) {fscanf (FP),"Name:%s C language score:%f database score:%f\n", &studentarr[i].name,&studentarr[i]. cscore,&Studentarr[i]. Databasescore); } fclose (FP); returnStudentarr;}//output Student Statistics to the consolevoidPutconsole (floatCscore,floatdatascore,student cmax,student cmin,student datamax,student datamin) {printf ("C-language average:%f database average score:%f\n", Cscore,datascore); printf ("C language scores highest:%s%f\nc language score lowest:%s%f\n", Cmax.name,cmax.cscore,cmin.name,cmin.cscore); printf ("database score Highest:%s%f\n database lowest score:%s%f\n", Datamax.name,datamax.cscore,datamin.name,datamin.cscore);}//output Student statistics to filevoidPutfile (Char*filepath,floatCscore,floatdatascore,student cmax,student cmin,student datamax,student datamin) {FILE*fp=fopen (FilePath,"a"); fprintf (FP,"C-language average:%f database average score:%f\n", Cscore,datascore); fprintf (FP,"C language scores highest:%s%f\nc language score lowest:%s%f\n", Cmax.name,cmax.cscore,cmin.name,cmin.cscore); fprintf (FP,"database score Highest:%s%f\n database lowest score:%s%f\n", Datamax.name,datamax.cscore,datamin.name,datamin.cscore); Fclose (FP);}//Statistical score InformationvoidStatic (intN,student *studentarr,float&cscore,float&datascore,student &cmax,student &cmin,student &datamax,student &datamin) {Cscore=0; Datascore=0; Cmax.cscore=0; Cmin.cscore=101; Datamax. Databasescore=0; Datamin. Databasescore=101; for(intI=0; i<n;i++) {Cscore+=Studentarr[i]. Cscore; Datascore+=Studentarr[i]. Databasescore; if(Cmax.cscore<studentarr[i]. Cscore) cmax=Studentarr[i]; if(Cmin.cscore>studentarr[i]. Cscore) cmin=Studentarr[i]; if(Datamax. Databasescore<studentarr[i]. Cscore) datamax=Studentarr[i]; if(Datamin. Databasescore>studentarr[i]. Cscore) datamin=Studentarr[i]; } Cscore=(float) cscore/N; Datascore=(float) datascore/N;}//Student Information ManagementvoidInfomanage (Char*filepath,student *studentarr,intN) {FILE*fp=fopen (FilePath,"W"); for(intI=0; i<n;i++) { Char*ar=Studentarr[i].name; fprintf (FP,"Name:%s C language score:%f database score:%f\n", Studentarr[i].name,studentarr[i]. Cscore,studentarr[i]. Databasescore); } floatCscore; floatDatascore; Student Cmax; Student Cmin; Student Datamax; Student datamin; Static (n,studentarr,cscore,datascore,cmax,cmin,datamax,datamin); Putconsole (cscore,datascore,cmax,cmin,datamax,datamin); Putfile (filepath,cscore,datascore,cmax,cmin,datamax,datamin);}//Recursive FunctionsvoidTest () {printf ("Please enter the number of students you would like to count:"); intN; scanf ("%d",&N); Char*putpath="E:\\mystudent.txt"; Student*studentarr=GetFile (putpath,n); //Student *studentarr=scanstudent (n);infomanage (putpath,studentarr,n); Test ();}voidMain () {test ();}
Basic operation of C language text file