鱼C论坛

 找回密码
 立即注册
查看: 3435|回复: 8

[技术交流] 文件的怪问题,大家进来看看怪不怪!

[复制链接]
发表于 2011-11-12 17:13:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.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);
                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[i],sizeof(struct data),1,fp)==0)
        {        printf("读取文件失败!");exit(0);}
        printf("账号%d:%s,密码:%s \n",i+1,person[i].account,person[i].code);
}
fclose(fp);
}
为什么当程序运行3遍之后,最后一遍却只能输出7个结构体数据,而不是9个呢?迷惑。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-11-12 17:16:06 | 显示全部楼层
为什么会这样啊!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-12 18:58:23 | 显示全部楼层
编译都编译不了,
gets那里是不是错了
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-12 19:01:22 | 显示全部楼层
还有你fwirte中的buffer是不是错了呢  &porsen还是&porsen[i]呢
对不起,我也是初学者,你自己看看吧!!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-12 19:09:31 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>


  4. struct data
  5. {
  6.     char account[20];
  7.     char code[20];
  8. };

  9. void main()
  10. {
  11.     struct data person[3];
  12.     int i=0;

  13.     FILE *fp;     //定义一个文件指针

  14.     if((fp=fopen("dat","ab"))==NULL)
  15.     {
  16.         printf("this file can not open!");
  17.         exit(0);
  18.     }        

  19.     while(i<3)
  20.     {
  21.         printf("请输入账号:");

  22.         gets(person[i].account); //接受的对象是person[i].account不是person.account

  23.         rewind(stdin);

  24.         printf("请设置密码:");

  25.         gets(person[i].code);   //同上

  26.         rewind(stdin);

  27.         if(fwrite(&person[i],sizeof(struct data),1,fp)!=0)
  28.         {
  29.             printf("保存成功!\n请牢记你得账号:%s\n密码密码为:%s\n",person[i].account,person[i].code);
  30.             i++;        
  31.         }
  32.         else
  33.             printf("保存失败,请重试!");
  34.     }

  35.     printf("\n你还有%d次的输入机会!\n",3-i);

  36.     fclose(fp);                        //关闭文件*/

  37.     if((fp=fopen("dat","rb"))==NULL)//重新打开文件,输出内容
  38.     {
  39.         printf("this file can not open!");
  40.         exit(0);
  41.     }

  42.     for(i=0;i<20;i++)
  43.     {
  44.         if(fread(&person,sizeof(struct data),1,fp)==0)
  45.         {
  46.             printf("读取文件失败!");
  47.             exit(0);
  48.         }

  49.         printf("账号%d:%s,密码:%s \n", i+1, person[i].account, person[i].code);
  50.     }

  51.     fclose(fp);
  52. }
复制代码
修改了一下,你试试看行不行!!
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-11-12 20:04:03 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-11-12 20:12:55 | 显示全部楼层
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.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);
                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[i],sizeof(struct data),1,fp)==0)
        {        printf("读取文件失败!");exit(0);}
        printf("账号%d:%s,密码:%s \n",i+1,person[i].account,person[i].code);
}
fclose(fp);
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-12 23:15:12 | 显示全部楼层
你自己看看你第一个代码的person有没有加[i]
我运行了可以的!!!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-17 12:35:09 From FishC Mobile | 显示全部楼层
其实根本不矛盾,是楼主没理解:-P
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 17:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表