|
发表于 2011-11-12 19:09:31
|
显示全部楼层
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- struct data
- {
- char account[20];
- char code[20];
- };
- void main()
- {
- struct data person[3];
- int i=0;
- FILE *fp; //定义一个文件指针
- if((fp=fopen("dat","ab"))==NULL)
- {
- printf("this file can not open!");
- exit(0);
- }
- while(i<3)
- {
- printf("请输入账号:");
- gets(person[i].account); //接受的对象是person[i].account不是person.account
- rewind(stdin);
- printf("请设置密码:");
- gets(person[i].code); //同上
- rewind(stdin);
- if(fwrite(&person[i],sizeof(struct data),1,fp)!=0)
- {
- printf("保存成功!\n请牢记你得账号:%s\n密码密码为:%s\n",person[i].account,person[i].code);
- i++;
- }
- else
- printf("保存失败,请重试!");
- }
- printf("\n你还有%d次的输入机会!\n",3-i);
- fclose(fp); //关闭文件*/
- if((fp=fopen("dat","rb"))==NULL)//重新打开文件,输出内容
- {
- printf("this file can not open!");
- exit(0);
- }
- for(i=0;i<20;i++)
- {
- if(fread(&person,sizeof(struct data),1,fp)==0)
- {
- printf("读取文件失败!");
- exit(0);
- }
- printf("账号%d:%s,密码:%s \n", i+1, person[i].account, person[i].code);
- }
- fclose(fp);
- }
复制代码 修改了一下,你试试看行不行!! |
|