#include #include #define STUCOUNT 5 typedef struct STU_INFO { char stuNo[7]; char stuName[11]; } STU_INFO; int main(int argc, char *argv[]) { FILE *in; STU_INFO info[STUCOUNT-1]; //STUCOUNT个学生 char *filePath = "C:/stu.dat"; if ((in = fopen(filePath, "r")) == NULL) { printf("open file error\n"); } int cnt = 0; while (cnt < STUCOUNT)//(!feof(in)) { fscanf(in, "%6s,%s", info[cnt].stuNo, info[cnt].stuName); cnt++; } STU_INFO tmpInfo; int i, j; for (i = 0; i < STUCOUNT; i++) { for (j = i; j < STUCOUNT; j++) { if (atol(info[i].stuNo) > atol(info[j].stuNo)) { tmpInfo = info[i]; info[i] = info[j]; info[j] = tmpInfo; } } } for (i = 0; i < STUCOUNT; i++) { printf("%s, %s\n", info[i].stuNo, info[i].stuName); } fclose(in); return 0; } 100001,张三 100005,李四 100002,王五 100004,帅哥 100000,美女