#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char choice;
struct {
char name[20];
int rn;
}read,write;
void wr(){
FILE *fp;
fp = fopen("record.txt","a+");
printf("Enter Your Name: ");
scanf("%s",write.name);
printf("Enter Your Roll Number: ");
scanf("%d",&write.rn);
fprintf(fp,"%s\t%d",write.name,write.rn);
fclose(fp);
menu();
}
void rd(){
FILE *fp;
fp = fopen("record.txt","r");
while(fscanf(fp,"%s %d",read.name,&read.rn)!= EOF){
printf("%s\t%d\n",read.name,read.rn);
}
fclose(fp);
getche();
menu();
}
void menu(){
system("cls");
printf("Do you want to read/write/exit the data R/W/E: ");
scanf("%c",&choice);
tolower(choice);
if(choice == 'w'){
wr();
}
else if(choice == 'r'){
rd();
}
else if(choice == 'e'){
exit(1);
}
}
int main() {
menu();
getche();
system("cls");
return 0;
}
我想在用户输入'e'时终止它,但是它在每次输入信息以及我读取数据后都终止。
表示每次读取或写入功能均起作用后,程序应再次询问其终止选择。
答案 0 :(得分:0)
您需要像这样使用for
或while
循环:
int main()
{
while(1) // while true
{
menu();
getche();
system("cls");
}
return 0;
}
您已经在exit(1)
的{{1}}中呼叫else if
,因此添加menu()
即可使用。