#include <stdio.h>
#include <conio.h>
#include <string.h>
#include<stdlib.h>
struct person{
char stdNumb [20];
char firstName [20],lastName [20], icPass [20], nationality [20], gender [20], dateOfBirth [20];
char contact [20], address [30];
};
void appendData(){
FILE *fp;
struct person obj;
fp=fopen("D:\\data.txt","a");
printf("\n============================");
printf("\n ADD");
printf("\n============================\n\n");
printf("Student Number: ");
scanf("%s",&obj.stdNumb);
printf("First Name: ");
scanf(" %[^\n]s",obj.firstName);
printf("Last Name: ");
scanf(" %[^\n]s",&obj.lastName);
printf("IC/ Passport: ");
scanf("%s",&obj.icPass);
printf("Nationality: ");
scanf("%s",obj.nationality);
printf("Gender: ");
scanf("%s",&obj.gender);
printf("Date of Birth: ");
scanf("%s",obj.dateOfBirth);
printf("Contact: ");
scanf("%s", &obj.contact);
printf("Address: ");
scanf(" %[^\n]s", &obj.address);
fprintf(fp,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address);
fclose(fp);
}
void editRecord()
{
FILE *fp;
struct person obj;
fp=fopen("D:\\data.txt","r");
printf("\n============================");
printf("\n EDIT");
printf("\n============================\n\n");
while(!feof(fp))
{
fscanf(fp,"%s\n%s\n",obj.firstName,&obj.stdNumb);
printf("%s\n%s\n",obj.firstName,obj.stdNumb);
}
fclose(fp);
getch();
}
void searchRecord()
{
FILE *fp;
struct person obj;
char number[20];
int totrec=0;
fp=fopen("D:\\data.txt","r");
printf("\n\n============================");
printf("\n SEARCH");
printf("\n============================");
printf("\nEnter student number to search : ");
scanf("%s",&number);
while(!feof(fp))
{
fscanf(fp,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address);
if(strcmpi(obj.stdNumb,number)==0){
printf("\nStudent Number : %s",obj.stdNumb);
printf("\n\nName : %s",obj.firstName);
totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}
void deleteRecord()
{
FILE *fp, *fdel;
struct person obj;
char number[20];
fflush(stdin);
printf("\n============================");
printf("\n DELETE");
printf("\n============================\n\n");
printf("Enter student number to delete :");
scanf("%s", number);
fp=fopen("D:\\student.txt","r");
fdel=fopen("D:\\del.txt","w");
while(fscanf(fp,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address)!=0);
{
if(stricmp(number, obj.stdNumb)!=0)
fprintf(fdel,"\n%s\n%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n", obj.stdNumb, obj.firstName, obj.lastName, obj.icPass, obj.nationality, obj.gender, obj.dateOfBirth, obj.contact, obj.address);
}
fclose(fp);
fclose(fdel);
remove("D:\\data.txt");
rename("D:\\del.txt","D:\\data.txt");
printf("Successully Deleted.");
getch();
}
void main()
{
char choice;
while(1)
{
printf("\n============================");
printf("\n MENU");
printf("\n============================");
printf("\n1. ADD");
printf("\n2. SEARCH");
printf("\n3. EDIT");
printf("\n4. DELETE");
printf("\n5. EXIT");
printf("\n============================");
printf("\n\n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice)
{
case'1' : //call append record
appendData();
break;
case'2' : //call search record
searchRecord();
break;
case'3' : //edit record
editRecord();
break;
case'4' : //Read all record
deleteRecord();
break;
case'5':
exit(0);
defaut:
printf("ss");
}
}
}
这是我的完整代码。我完成了追加和搜索。问题是,当我尝试运行并删除记录时,输入学号后它不起作用。如何解决这个问题?