qq_32291935
2015-10-25 03:52
采纳率: 100%
浏览 1.5k
已采纳

菜鸟求解(C语言文件读写)

#include

#include

#include

#include

struct book

{

char name[20];

char phone[20];

char qq[20];

char wechat[20];

};

struct book members[50];

int a,j,k,l,a1,i=0;

char js[10]="y";

char da[20];

char m1[10];

char m2[20];

char m3[20];

char m4[20]="";

int main()

{

FILE * pf;

loop:

printf(" *************************************\n");

printf(" What do you want to do\n");

printf(" 1.creat\n");

printf(" 2.list\n");

printf(" 3.search\n");

printf(" 4.delete\n");

printf(" 5.close\n");

printf(" *************************************\n");

a=getch();

switch(a)

{

case 49 :

pf=fopen("txl.txt","a");

loop2:

printf(" *************************************\n");

printf(" please input information:\n");

printf(" input 'y' to quit when done\n");

printf(" *****************************\n");

printf(" name:");

scanf("%s",members[i].name);

if(strcmp(members[i].name,js)==0)

{

printf(" *****************************\n");

printf(" DATE SAVED\n");

printf(" *****************************\n");

getch();

system("cls");

fclose(pf);

goto loop2;

}

else

{

fprintf(pf,"*************************************\n");

fprintf(pf,"name:%s\n",members[i].name);

printf(" phone:");

scanf("%s",members[i].phone);

fprintf(pf,"phone:%s\n",members[i].phone);

printf(" qq:");

scanf("%s",members[i].qq);

fprintf(pf,"qq:%s\n",members[i].qq);

printf(" wechat:");

scanf("%s",members[i].wechat);

fprintf(pf,"wechat:%s\n",members[i].wechat);

i++;

system("cls");

fclose(pf);

goto loop;

}

case 50 :

pf=fopen("txl.txt","r");

for(a1=0;a1<50;a1++)

{

if(strcmp(m4,members[a1].name)==0)

{

break;

}

printf(" name:%s\n",members[a1].name);

printf(" phone:%s\n",members[a1].phone);

printf(" qq:%s\n",members[a1].qq);

printf(" wechat:%s\n",members[a1].wechat);

printf(" *************************************\n");

}

fclose(pf);

getch();

system("cls");

goto loop;

case 51 :

pf=fopen("txl.txt","r");

printf(" *************************************\n");

printf(" who do you want to find?\n");

printf(" *************************************\n");

printf(" name:");

scanf("%s",m1);

for(j=0;j<1024;j++)

{

if(strcmp(m1,members[j].name)==0)

{

printf(" phone:%s\n",members[j].phone);

printf(" qq:%s\n",members[j].qq);

printf(" wechat:%s\n",members[j].wechat);

getch();

system("cls");

fclose(pf);

goto loop;

}

}

break;

case 52 :

pf=fopen("txl.txt","r+");

printf(" *************************************\n");

printf(" who do you want to delete?\n");

printf(" *************************************\n");

printf(" name:");

scanf("%s",m2);

for(k=0;k<100;k++)

{

if(strcmp(members[k].name,m2)==0)

{

printf(" phone:%s\n",members[k].phone);

printf(" qq:%s\n",members[k].qq);

printf(" wechat:%s\n",members[k].wechat);

printf(" *************************************\n");

printf(" Are you sure(Y OR N):");

scanf("%s",m3);

if(strcmp(m3,js)==0)

{

for(l=k;l<49;l++)

{

members[l]=members[l+1];

}

printf(" *************************************\n");

printf(" This guy has been deleted\n");

printf(" *************************************\n");

getch();

system("cls");

fclose(pf);

goto loop;

}

}

}

case 53 :

pf=fopen("txl.txt","a");

fclose(pf);

}

}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qq_32291935 2015-10-25 03:53
    已采纳

    第一次运行生成的exe文件可以正常录入信息,txt文件上也有相应的数据,但是第二次打开exe文件数据就全部丢失了

    已采纳该答案
    打赏 评论
  • havedream_one 2015-10-25 04:25

    pf=fopen("txl.txt","a");改成a+试试

    打赏 评论

相关推荐 更多相似问题