?? modnum.c
字號:
/* modnum.c :修改學生信息 */
#include "stdio.h"
void ModifyByNumber()
{
int i,j,k;
long modnum; /*存儲教師輸入的要修改的學生學號*/
/*輸入各項修改后信息*/
long Number;
char Name[20];
float chinesescore;
float mathscore;
float heji;
student TmpS; /*定義進行操作時的臨時結構體變量*/
student s[SIZE];/*SIZE,在shead.h頭文件中定義的常量,值為100 */
int recNumber;
char DataFile[40] = "",next;
/*DataFile存儲學生信息的文件名,next為是否進行下一次刪除操作的選項*/
FILE *fp;/*====fp指針指向存儲數據的文件名====*/
/*提示教師輸入要進行修改記錄的文件名*/
printf("\nplease input the name of file where data is stored,end with enter key.\n");
gets(DataFile);
/*提示教師輸入要進行修改記錄的文件名*/
while(*DataFile == ('\0'))
{
printf("\nplease input the name of file where data is stored,end with enter key.\n");
gets(DataFile);
}
begin:
/*以讀的方式打開文件,如文件不存在,提示錯誤*/
fp=fopen(DataFile,"rb");
if (fp == NULL)
{
printf("\nOpen file %s fail!End with any key\n",DataFile);
perror("Open file fail");
getch();
exit(1);
}
printf("please input the Employee'seatnum which you will modify:");
scanf("%ld",&modnum);
printf("the student you will delete is:%ld\n",modnum);
/*輸入要修改記錄的各項內容值*/
Number=modnum;
printf("name=");
scanf("%s",Name);
printf("chinesescore=");
scanf("%f",&chinesescore);
printf("mathscore=");
scanf("%f",&mathscore);
/*用公式自動計算學生總分*/
heji=chinesescore+mathscore;
/*將文件中要修改的信息存入結構體數組*/
recNumber=0;
/*循環將文件數據讀入結構體數組,
如文件中的數據學生號和要修改的學生號不符,則原樣寫入數組,
如文件中數據的學生號和要修改學生號匹配,
則根據教師輸入的各項修改內容重新賦值,即修改,并寫入數組*/
while((fread(&TmpS,sizeof(student),1,fp)) != (int)NULL)
{
if(TmpS.Number!=modnum)
{
s[recNumber].Number = TmpS.Number;
strcpy(s[recNumber].Name, TmpS.Name);
s[recNumber].chinesescore = TmpS.chinesescore;
s[recNumber].mathscore = TmpS.mathscore;
s[recNumber].heji = TmpS.heji;
recNumber++;
}
else
{
s[recNumber].Number = Number;
strcpy(s[recNumber].Name, Name);
s[recNumber].chinesescore = chinesescore;
s[recNumber].mathscore = mathscore;
s[recNumber].heji = heji;
recNumber++;
}
}
fclose(fp);
/*====將修改后的結構體數組記錄寫入文件====*/
fp=fopen(DataFile,"wb+");
if (fp == NULL)
{
printf("\nSet up file %sfail !end with anykey.\n",DataFile);
perror("Set up fail");
getch();
exit(1);
}
for(i=0; i<recNumber; i++)
{
if(fwrite(&s[i],sizeof(student),1,fp)!=1)
{
printf("\nWrite file %s fail!end with anykey.\n",DataFile);
perror("Write file fail!");
getch();
exit(1);
}
}
fclose(fp);
/*====顯示修改后的文件====*/
fp=fopen(DataFile,"rb");
if (fp == NULL)
{
printf("\nOpen file%sfail!End with any key \n",DataFile);
perror("Open file fail");
getch();
exit(1);
}
printf("the file after modify is:\n");
printf("\nNumber \t\tName\tchinesescore\tmathscore\theji\n");
while(fread(&TmpS,sizeof(student),1,fp) != (int)NULL)
{
if(TmpS.Number!=0)
printf("\n%ld\t %s\t %5.2f\t %5.2f\t%5.2f\n",TmpS.Number,TmpS.Name,TmpS.chinesescore,TmpS.mathscore,TmpS.heji);
}
fclose(fp);
/*提示是否進行下一次修改*/
printf("\nGo on ?(y/n)");
next=getche();
putchar('\n');
if ( next =='y' || next =='Y') goto begin;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -