用fgets到达EOF

时间:2013-03-18 18:23:55

标签: c unix eof fgets

我正在编写一个执行一些身份验证操作的函数。我有一个文件,所有user_id:password:flag对夫妻的结构如下:

Users.txt

user_123:a1b2:0 user_124:a2b1:1 user_125:a2b2:2

这是代码:

int main(){
    /*...*/

    /*user_id, password retrieving*/
    USRPSW* p = malloc(sizeof(USRPSW));
    if(p == NULL){
        fprintf(stderr, "Dynamic alloc error\n");
        exit(EXIT_FAILURE);
    }
    memset((void*)p, 0, sizeof(USRPSW));

    if(usr_psw_read(acc_sock_ds, p->user_id, USR_SIZE) <= 0){
        printf("Failed read: connection with %s aborted.\n",
                 inet_ntoa(client_addr.sin_addr));
        close(acc_sock_ds);
        continue;
    }

    if(usr_psw_read(acc_sock_ds, p->password, PSW_SIZE) <= 0){
        printf("Failed read: connection with %s aborted.\n",
                 inet_ntoa(client_addr.sin_addr));
        close(acc_sock_ds);
        continue;
    }

    /*Authentication through user_id, password*/
    FILE *fd;
    fd = fopen(USERSFILE, "r");
    if(fd == NULL){
        fprintf(stderr, "Users file opening error\n");
        exit(EXIT_FAILURE);
    }

    char *usr_psw_line = malloc(USR_SIZE+PSW_SIZE+3+1);
    if(usr_psw_line == NULL){
        fprintf(stderr, "Dynamic alloc error\n");
        exit(EXIT_FAILURE);
    }

    while(1){

        memset((void*)usr_psw_line, 0, sizeof(USR_SIZE+PSW_SIZE+3+1));
        fgets(usr_psw_line, USR_SIZE+PSW_SIZE+3+1, fd);
        printf("%s\n", usr_psw_line);
        fseek(fd, 1, SEEK_CUR);


        /*EOF management*/
        /*usr_id - password matching checking */

    }   
/*...*/    
}

如何管理EOF到达?我看到当到达EOF时fgets不再编辑usr_psw_line但是都没有返回NULL指针。如果达到EOF,则意味着在用户文件中找不到匹配并且循环中断。

有人可以给我一些提示或建议吗?

2 个答案:

答案 0 :(得分:20)

fgets()在到达文件结尾或错误条件时返回空指针。

EOF是一个宏,它指定在类似条件下某些其他函数返回的值;它不仅仅是短语“end of file”的缩写。)

您忽略了fgets()返回的结果。不要那样做。

请注意,仅检查feof(fd)将无法执行您想要的操作。如果您已到达文件末尾,feof()将返回true结果。如果您遇到错误,feof()仍然会返回false,如果您使用feof()来决定何时完成,那么您将获得无限循环。并且在之后你没有读取输入时它才会返回。

大多数C输入函数返回一些特殊值,表示没有什么可读的。 fgets()NULLfgetc()EOF,依此类推。如果您愿意,可以在之后致电feof()和/或ferror()以确定为什么没有其他内容可供阅读。

答案 1 :(得分:3)

您可能想在循环中尝试这样的事情:

while(1)
{
    memset((void*)usr_psw_line, 0, sizeof(USR_SIZE+PSW_SIZE+3+1));
    if( !fgets(usr_psw_line, USR_SIZE+PSW_SIZE+3+1, fd)
     || ferror( fd ) || feof( fd ) )
    {
        break;
    }
    printf("%s\n", usr_psw_line);
    fseek(fd, 1, SEEK_CUR);

    /*EOF management*/
    /*usr_id - password matching checking */

}

使用额外的代码,如果fgets返回NULL(无需读取更多数据)或者您读取了EOF标记或文件有任何错误,循环将终止。我确信它有点矫枉过正,但这些测试一直对我有用。