读书人

关于文件读取和写入的有关问题

发布时间: 2012-04-14 17:14:21 作者: rapoo

关于文件读取和写入的问题
我想测试下一个登录的函数,如果初始文件不存在,就建立一个文件,把初始帐号密码写入。如果有文件就一直读取,知道帐号密码匹配,我的代码当帐号密码正确时能够成功,如果帐号密码不正确,它就一直在最后那里循环,跳不出来,可是那里我明明加了判断是否到文件尾的函数。代码如下

C/C++ code
#include <stdlib.h>#include <stdio.h>struct login{    int account;    int password;};int main(){    long acc;    long pass;    FILE *fp;    login temp;    scanf("%ld",&acc);    scanf("%ld",&pass);    if((fp=fopen("login.dat","rb"))==NULL){        fclose(fp);        fp=fopen("login.dat","wb");        login super;        super.account=123456;//初始帐号        super.password=111;//初始密码        fwrite(&super,sizeof(login),1,fp);        fclose(fp);        fp=fopen("login.dat","rb");    }        fread(&temp,sizeof(login),1,fp);    while(temp.account!=acc||temp.password!=pass){        fread(&temp,sizeof(login),1,fp);        if(!feof(fp)){            printf("帐号或密码错误!");            return 0;        }    }    printf("登录成功");    return 1;}


[解决办法]
你写的是:如果文件还没结束就退出,如果已经结束了就继续循环......
[解决办法]
int feof(
FILE* stream
);
Parameters
stream
Pointer to FILE structure.
Return Values
The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file.

It returns 0 if the current position is not end of file.

There is no error return.

读书人网 >C语言

热点推荐