源文件:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "student.h" void main() { int num_order; char c; num_order = msg();//輸出相關信息,獲取相關操作 while( num_order!=1 && num_order!=2 && num_order!=3) { printf("請重新輸入正確參數:\n"); scanf("%d",&num_order); } switch( num_order ) { case 1: from_file(0); break;//查詢 case 2: input_(); break;//錄入 case 3: from_file(1); break;//修改 } c = getchar();//用於接受上一個回車符 printf("\n是否執行其他操作?\n是/Y、否/N\n"); c = getchar(); while( c == 89 || c == 121 ) { printf("請輸入相關參數\n"); scanf("%d",&num_order); switch( num_order ) { case 1: from_file(0); break;//查詢 case 2: input_(); break;//錄入 case 3: from_file(1); break;//修改 } c = getchar(); printf("\n是否執行其他操作?\n是/Y、否/N\n"); c = getchar(); } }
#include <stdio.h> #include <string.h> #include <stdlib.h> int msg();//信息提示窗口 void input_();//錄入相關信息 void save_files( struct Student_info *p );//保存內容至文件 void print_( struct Student_info *p, int n );//輸出內容到屏幕 void from_file( int flag );//讀取文件內容 void change_info( struct Student_info *p, int loop );//修改相關內容 void sort_info(struct Student_info *p);//排序 struct Student_info { char name[10]; int id; int age; char adress[30]; }; int num_stud; int msg()//信息提示窗口 { int n; printf("\t\t\t\t學生管理系統\n\n"); printf("\t\t 請輸入你想執行的操作:\n"); printf("\t\t ╭════════════════╮ \n"); printf("\t\t║1.學生信息查詢 ║\n"); printf("\t\t║2.學生信息錄入 ║\n"); printf("\t\t║3.學生信息修改 ║\n"); printf("\t\t╰═════════════════╯\n"); scanf("%d",&n); return(n); } void input_()//錄入相關信息 { int i; struct Student_info *p,*into_save; system("cls"); printf("學生信息錄入系統:\n"); printf("請輸入想要錄入信息的學生人數\n"); scanf("%d",&num_stud); if((into_save = p = ( struct Student_info *)malloc( num_stud * sizeof( struct Student_info ))) == NULL ) printf("創建空間失敗!\n"); else printf("依次輸入學生學號 姓名 年齡 地址\n"); for( i = 0; i < num_stud ; i++, p++ ) { scanf( "%d %s %d %s", &p->id, p->name, &p->age, p->adress); } save_files( into_save ); } void save_files( struct Student_info *p )//內容保存 { int i; FILE *fp; if( (fp = fopen( "stud.dat", "ab" )) != NULL ) { for( i = 0; i < num_stud; i++, p++ ) { fwrite( p , sizeof(struct Student_info), 1, fp); } fclose( fp ); printf( "信息錄入完成!\n" ); } else if( (fp = fopen( "stud.dat", "wb" )) == NULL ) { printf( "文件創建失敗!\n"); exit(0); } else { for( i = 0; i < num_stud; i++, p++ ) { fwrite( p , sizeof(struct Student_info), 1, fp); } fclose( fp ); printf( "信息錄入完成!\n" ); } } void print_( struct Student_info *p, int n )//輸出屏幕 { int i; printf( "學號\t姓名\t年齡\t地址\n\n"); for( i = 0; i < n ; i++, p++ ) { printf( "%-4d %7s %6d %8s\n", p->id, p->name, p->age, p->adress); } printf("\n"); } void from_file( int flag )//讀取文件內容 { FILE *fp; struct Student_info temp; struct Student_info *p,*into_print,*first; int i = 0; int loop = 0; system("cls"); printf("\t學生信息查詢 \n"); if((fp = fopen("stud.dat","rb")) == NULL) { printf( "文件信息不存在\n" ); exit(0); } while( !feof(fp) ) { fread( &temp, sizeof(struct Student_info), 1, fp); loop++; } loop-=1;//獲取文件中內容個數 rewind(fp); first = into_print = p = (struct Student_info *)malloc(loop*sizeof(struct Student_info)); for( i = 0; i < loop; i++, p++ ) { fread( p, sizeof(struct Student_info), 1 ,fp); } print_( into_print, loop ); if( flag == 0 ) { free( first ); first = into_print = p = NULL; } else { change_info( first, loop ); } fclose(fp); } void change_info( struct Student_info *p, int loop ) { int n,i; struct Student_info *t1,*t2; FILE *fp; printf("請輸入要修改學生信息的學號:\n"); scanf("%d",&n); t2 = t1 = p; for( i = 0; i < loop; i ++, t1++ ) { if( t1->id == n) break; } scanf( "%d %s %d %s", &t1->id, t1->name, &t1->age, t1->adress); if( ( fp = fopen( "stud.dat", "wb") ) == NULL ) { printf("文件寫入錯誤!\n"); } else { for( i = 0; i < loop; i ++, p++ ) { fwrite( p, sizeof(struct Student_info), 1, fp ); } } fclose(fp); printf("修改成功!\n"); free(t2); }
1.首頁
2.錄入界面
3.查詢界面
4.修改界面
注意看學號 102 的信息 已經改變
附上文件的鏈接:
鏈接:http://pan.baidu.com/s/1sjjSpy9 密碼:kowr