到达EOF用于fgets [英] Reaching EOF with fgets

查看:150
本文介绍了到达EOF用于fgets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写执行一些动作认证功能。我有所有的文件的user_id:密码:标志夫妇结构是这样的:

I'm writing a function that perform some authentications actions. I have a file with all the user_id:password:flag couples structured like this:

Users.txt

Users.txt

user_123:A1B2:0 user_124:A2B1:1 user_125:A2B2:2

这是code:

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但既不是返回一个空指针。如果达到EOF它意味着不匹配是在用户文件和环断裂找到。

How can I manage the EOF reaching? I saw that when EOF is reached fgets doesn't edits anymore the usr_psw_line but neither returns a NULL pointer. If EOF is reached it mean that no match are found in the users file and the loop breaks.

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

Can someone give me some tips or suggests?

推荐答案

与fgets()返回一个空指针当它达到结束的文件或错误条件。

fgets() return a null pointer when it reaches end-of-file or an error condition.

EOF 是指定由在类似条件下某些其他功能的返回值的宏;它不只是对那句文件结尾的缩写)

(EOF is a macro that specifies the value returned by certain other functions in similar conditions; it's not just an abbreviation for the phrase "end of file".)

您是无视返回的结果与fgets()。不这样做。

请注意,只是检查的feof(FD)不会做你想要的。如果你已经达到了文件的末尾的feof()返回一个真实结果。如果你遇到一个错误,而不是,的feof()还是返回false,而你如果你使用了自己的无限循环的feof() 当你做决定。而且它不会返回true,直到的之后的你无法读取输入。

Note that just checking feof(fd) won't do what you want. feof() returns a true result if you've reached the end of the file. If you encounter an error instead, feof() still returns false, and you've got yourself an infinite loop if you're using feof() to decide when you're done. And it doesn't return true until after you've failed to read input.

大多数C输入函数返回一些特殊的值,表示有什么更多的阅读。对于与fgets() NULL 龟etc()这是 EOF ,等等。如果你喜欢,你可以叫的feof()和/或 FERROR()事后确定的为什么的有什么更多的阅读。

Most C input functions return some special value to indicate that there's nothing more to read. For fgets() it's NULL, for fgetc() it's EOF, and so forth. If you like, you can call feof() and/or ferror() afterwards to determine why there's nothing more to read.

这篇关于到达EOF用于fgets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆