鱼C论坛

 找回密码
 立即注册
查看: 3067|回复: 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个呢?迷惑。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-11-12 17:16:06 | 显示全部楼层
为什么会这样啊!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-11-12 18:58:23 | 显示全部楼层
编译都编译不了,
gets那里是不是错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-11-12 19:01:22 | 显示全部楼层
还有你fwirte中的buffer是不是错了呢  &porsen还是&porsen[i]呢
对不起,我也是初学者,你自己看看吧!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 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);
}
修改了一下,你试试看行不行!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-11-12 20:04:03 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> 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);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-11-12 23:15:12 | 显示全部楼层
你自己看看你第一个代码的person有没有加[i]
我运行了可以的!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-11-17 12:35:09 From FishC Mobile | 显示全部楼层
其实根本不矛盾,是楼主没理解:-P
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-9-21 11:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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