//readtest.c --- @copyLeft by tsaiwn@csie.nctu.edu.tw // This program reads data from studat.txt .. // .. into an array which is actually in Heap memory area. // This program also shows you how to use struct to group data. // Since we do NOT know how large the memory required, // we have to read ALL data before allocating the memory. // We use "malloc" to allocate an array for student data. #define MAX_STU 99999 #define fileName "studat.txt" #include #include typedef struct { long id; int h, m, q, f; double total; // 學期總成績= (h*0.2 + m*0.3 + q*0.1+f*0.4); long rank, recno; // rank, original record number char name[9]; // student's name } Student_t; // 這種命名方式是不錯的習慣 /// long aid[MAX_STU]; // Students ID int as1[MAX_STU], as2[MAX_STU], as3[MAX_STU], as4[MAX_STU]; long arank[MAX_STU], arec[MAX_STU]; double atotal[MAX_STU]; char aname[MAX_STU][9]; // Students name long nOfStu; long countF(char*fName); // count records in fName Student_t * y; // a pointer to a Student_t void readOne( ), readTwo(Student_t x[ ], char*fileNM); void printRec(Student_t); // print one Student int main( ) { long k; FILE* fp = fopen(fileName, "rt"); // read, text if(fp==0) {printf(" %s NOT found!",fileName); getchar( );return 1;} fclose(fp); // close it since I just want to make sure it's OK readOne( ); // read into all data array printf("First: %8d %8s %3d %3d %3d %3d %5.2f\n", aid[0], aname[0], as1[0], as2[0], as3[0], as4[0], atotal[0]); k = nOfStu -1; // last student printf(" Last: %8d %8s %3d %3d %3d %3d %5.2f\n", aid[k], aname[k], as1[k], as2[k], as3[k], as4[k], atotal[k]); nOfStu = countF(fileName); /// then, /// allocate memory for y[ ] which will store all students y = (Student_t *) malloc(sizeof(Student_t) * nOfStu); printf("=== === Now test readTwo( )\n"); readTwo(y, fileName); // read into y[ ], which is in Heap area printf(" 1st_record: "); printRec(y[0]); printf(" 2nd_record: "); printRec(y[1]); printf(" 3rd_record: "); printRec(y[2]); printf(" ... then, the last 3 records:\n"); for(k= nOfStu-3; k < nOfStu; k++) { printf(" %6ld_rec: ", k+1); // 從 1 算起所以要 + 1 printRec(y[k]); // y[k]; 注意這只是 "第幾筆" 叫法不同 }// for k fprintf(stderr, "\nHit ENTER key..."); getchar( ); return 0; } // main( void readOne( ) { // 讀到各個 Array, 不使用 struct FILE* fp = stdin; // file pointer long id; char name[9]; int s1, s2, s3, s4, kk; long rank, recno=0; static char buf[999]; fp = fopen(fileName, "rt"); // read, text if(fp==0) return; // Error when open fgets(buf, sizeof(buf), fp); // stdin printf("Line1: %s", buf); kk=0; while( ! feof(fp) ) { sscanf(buf,"%ld %s %d %d %d %d", &id, name, &s1, &s2, &s3,&s4); aid[kk]=id; strcpy(aname[kk], name); as1[kk]=s1; as2[kk]=s2; as3[kk]=s3; as4[kk]=s4; ++kk; buf[0]=0; fgets(buf, sizeof(buf), fp); } if(buf[0] !=0 ) { fprintf(stderr, " Warning: last line has no new line.\n"); // 還有ㄟ, 須抓出資料塞入各 array [kk] ++kk; // count it ! 當然這也要算喔 ! } nOfStu = kk; // global variable nOfStu fprintf(stderr, "Total %d records.\n", kk); }// readOne( long countF(char* fnm) { // how many records in file fnm long kk = 0; static char buf[999]; FILE* fp = fopen(fnm, "rt"); // read, text if(fp==0) return 0; // open error, nothing read buf[0]=0; // ensure the buffer is empty before read fgets(buf, sizeof buf, fp); while( ! feof(fp) ) { ++kk; buf[0]=0; fgets(buf, sizeof(buf), fp); } fclose(fp); // close it if(buf[0] !=0 ) { // got something in buf[ ] fprintf(stderr, " Warning: last line has no new line.\n"); ++kk; // count this record } return kk; }//countF void readTwo(Student_t * x, char*fnm) { //有使用 struct; 讀檔案 fnm static char buf[999]; long i = 0; Student_t tmp; // temporary student of struct Student_t FILE* fp = fopen(fnm, "rt"); // read, text if(fp == 0) return; // Open Fail ! for(;;) { buf[0]=0; fgets(buf, sizeof buf, fp); //read one record if(feof(fp))break; // EOF encounted sscanf(buf,"%ld %s %d %d %d %d", &tmp.id, tmp.name, &tmp.h, &tmp.m, &tmp.q,&tmp.f); tmp.total = tmp.h*0.2 + tmp.m*0.3 + tmp.q*0.1+ tmp.f*0.4; x[i++] = tmp; // 把該 struct tmp 放入 第 i 學生 x[i] }// for if(buf[0]) { // one more record in buf[ ] ; 最後一列沒 NewLine sscanf(buf,"%ld %s %d %d %d %d", &tmp.id, tmp.name, &tmp.h, &tmp.m, &tmp.q,&tmp.f); tmp.total = tmp.h*0.2 + tmp.m*0.3 + tmp.q*0.1+ tmp.f*0.4; x[i++] = tmp; // 這最後一個也要喔 ! } } // readTwo( void printRec(Student_t s) { // print the Student s printf("%8ld %8s %3d %3d %3d %3d %5.2f %6ld", s.id, s.name, s.h, s.m, s.q, s.f, s.total, s.rank); printf("\n"); // 學期總成績= (h*0.2 + m*0.3 + q*0.1+f*0.4); } // printRec( /****** Line1: 9938596 章LUf 76 74 79 98 Total 81482 records. First: 9938596 章LUf 76 74 79 98 0.00 Last: 9998482 林OCh 5 94 90 77 0.00 === === Now test readTwo( ) 1st_record: 9938596 章LUf 76 74 79 98 84.50 1 2nd_record: 9934067 趙NLv 72 64 61 94 77.30 1 3rd_record: 9947865 魏NEu 86 98 58 90 88.40 1 ... then, the last 3 records: 81480_rec: 9998480 馬BNt 75 79 71 67 72.60 1 81481_rec: 9998481 馬GVn 74 57 61 86 72.40 1 81482_rec: 9998482 林OCh 5 94 90 77 69.00 1 Hit ENTER key... *****************/