問題描述:
在文件studd.txt中存放學生信息,學生信息包含學號、姓名和成績。要求採用菜單形式實現學生記錄的創建、添加、查找(按學號進行)、修改(按學號進行)和刪除(按學號進行)、顯示所有信息等功能。用戶可以循環操作直到選擇退出為止。
分析:
本題是對文件的綜合應用,採用菜單形式可以方便地實現程序模塊的設計方法,這樣可以使程序顯得簡潔明了。設計時可以逐個完成各模塊功能,並調試好每個模塊,然後再整合各模塊。
參考代碼:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct student
{ char no[10];
char name[20];
int score;
};
char filename[100]=”studd.txt”; /*設置文件名*/
FILE *fp;
void create(); /*創建函數聲明*/
void append(); /*添加函數聲明*/
void search(); /*查找函數聲明*/
void del(); /*刪除函數聲明*/
void modify(); /*修改函數聲明*/
void output(); /*顯示函數聲明*/
int main(void)
{
int num;
while(1)
{
printf(” ***學生成績系統***\n\n”);
printf(” 1.創建記錄 \n”);
printf(” 2.添加記錄 \n”);
printf(” 3.查找記錄 \n”);
printf(” 4.修改記錄 \n”);
printf(” 5.刪除記錄 \n”);
printf(” 6.顯示記錄 \n”);
printf(” 0.退出系統 \n”);
printf(“\n 選擇序號0-6:” );
scanf(“%d”,&num);
if(num>=0&&num<=6)
{
switch(num)
{ case 1:create();break;
case 2:append();break;
case 3:search();break;
case 4:modify();break;
case 5:del();break;
case 6:output();break;
case 0:exit(1);
}
printf(“\n\n操作完畢,請再次選擇!\n”);
}
else
printf(“\n\n選擇錯誤,請再次選擇!\n”);
}
getch();
return 0;
}
/*創建記錄*/
void create()
{
struct student stu;
if((fp=fopen(filename,”w”))==NULL)
{
printf(“Cannot Open File!\n”);
exit(0);
}
fprintf(fp,”%-10s%-20s%-50s\n”,”學號”,”姓名”,”成績”);
printf(“\n請輸入學號、姓名及成績(以0結束)\n”);
scanf(“%s”,stu.no);
while(strcmp(stu.no,”0″))
{
scanf(“%s %d”,stu.name,&stu.score);
fprintf(fp,”%-10s%-20s%-50d\n”,stu.no,stu.name,stu.score);
scanf(“%s”,stu.no);
}
fclose(fp);
}
/*添加記錄*/
void append()
{
struct student stu;
if((fp=fopen(filename,”a”))==NULL)
{
printf(“\nCannot Open File!”);
exit(0);
}
printf(“\n請輸入要添加的學號、姓名及成績\n”);
scanf(“%s%s%d”,stu.no,stu.name,&stu.score);
fprintf(fp,”%-10s%-20s%-50d\n”,stu.no,stu.name,stu.score);
fclose(fp);
}
/*查找記錄*/
void search()
{
int k=0;
char nokey[10];
struct student stu;
printf(“\n請輸入學號:”);
scanf(“%s”,nokey);
if((fp=fopen(filename,”r”))==NULL)
{
printf(“\nCannot Open File!”);
exit(0);
}
fseek(fp,1L*sizeof(struct student),0);
while(!feof(fp))
{
fscanf(fp,”%s%s%d”,stu.no,stu.name,&stu.score);
if(strcmp(nokey,stu.no)==0)
{
printf(“\n\n已查找到,該記錄為:\n\n”);
printf(“%-10s%-20s%-50s”,”學號”,”姓名”,”成績”);
printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);
k=1;
}
}
if(!k)
printf(“\n文件中無此人的記錄。”);
fclose(fp);
}
/*修改記錄*/
void modify()
{
int k=0;
long position;
char nokey[10];
struct student stu;
printf(“\n請輸入學號:”);
scanf(“%s”,nokey);
if((fp=fopen(filename,”r+”))==NULL)
{
printf(“\nCannot Open File!”);
exit(0);
}
fseek(fp,1L*sizeof(struct student),0);
while(!feof(fp))
{
fscanf(fp,”%s%s%d”,stu.no,stu.name,&stu.score);
if(strcmp(nokey,stu.no)==0)
{ position=ftell(fp);
k=1;
break;
}
}
if(k)
{
printf(“\n\n已查找到,該記錄為:\n\n”);
printf(“%-10s%-20s%-50s”,”學號”,”姓名”,”成績”);
printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);
printf(“\n請輸入新的學號、姓名及成績:”);
scanf(“%s%s%d”,stu.no,stu.name,&stu.score);
fseek(fp,position-1L*sizeof(struct student),SEEK_SET);
fprintf(fp,”\n%-10s%-20s%-50d”,stu.no,stu.name,stu.score);
}
else
printf(“\n\n文件中無此人的記錄。”);
fclose(fp);
}
/*刪除記錄*/
void del()
{
int m,k=0;
long position;
char nokey[10];
struct student stu;
printf(“\n請輸入學號:”);
scanf(“%s”,nokey);
if((fp=fopen(filename,”r+”))==NULL)
{
printf(“\nCannot Open File!”);
exit(0);
}
fseek(fp,1L*sizeof(struct student),0);
while(!feof(fp))
{
fscanf(fp,”%s%s%d”,stu.no,stu.name,&stu.score);
if(strcmp(nokey,stu.no)==0)
{ position=ftell(fp);
k=1;
break;
}
}
if(k)
{
printf(“\n\n已查找到,該記錄為:\n\n”);
printf(“%-10s%-20s%-50s”,”學號”,”姓名”,”成績”);
printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);
printf(“\n確實要刪除記錄,請按1;不刪除記錄,請按0:”);
scanf(“%d”,&m);
if(m)
{
fseek(fp,position-1L*sizeof(struct student),SEEK_SET);
fprintf(fp,”%-10s%-20s%-50s”,””,””,””);
}
}
else
printf(“\n\n文件中無此人的記錄。”);
fclose(fp);
}
/*顯示記錄*/
void output()
{
struct student stu;
if((fp=fopen(filename,”r”))==NULL)
{
printf(“\nCannot Open File!”);
exit(0);
}
printf(“\n\n文件內容為:\n”);
fseek(fp,1L*sizeof(struct student),0);
while(!feof(fp))
{
fscanf(fp,”%s%s%d\n”,stu.no,stu.name,&stu.score);
printf(“%-10s%-20s%-50d”,stu.no,stu.name,stu.score);
}
fclose(fp);
}
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/222171.html
微信掃一掃
支付寶掃一掃