Entire file is not read in C (EOF occurs unexpectedly)

前端 未结 2 538
长发绾君心
长发绾君心 2021-01-24 09:56

I am trying to print contents of a file with approximately 4000 characters. Somehow the program records only the first 220 characters and terminates.

int main(vo         


        
相关标签:
2条回答
  • 2021-01-24 10:51

    I think the code is fine, apart from the fact that you should change the ch to int.

    fgetc() returns

    • If success, "the character read as an unsigned char cast to an int"
    • In failure, "EOF on end of file or error"

    So, first, you have to change the ch to int, as some return values from fgetc() may not fit into a char.

    Now, in the second case, you're not checking the return value of fgetc() against EOF to detect any error . You're simply taking the return value and trying to store those values into the array. Actually, when the end of file is reached, there is nothing more to be read, and all the further reads on the same file pointer will return you error.

    It is most likely that those values. after 220 in your case are valid , at all.

    So, to the statement in your question,

    (EOF occurs unexpectedly)

    is wrong. It occurs just fine, you're ignoring it and running into, well, trouble.


    Note:

    1. In your first snippet, you're doing two successive fgetc() calls, essentially discarding the result of the first one, and using the second one without any check.
    2. Never use gets(). It suffers from buffer overflow issues. Always use fgets() instead.
    0 讨论(0)
  • 2021-01-24 10:55

    the following code:

    uses fgets() rather than the obsolete gets()
    removed the newline from the user input
    uses the proper size_t rather than int for indexing
    is consistently indented
    has modification to the printf statements to display size_t rather than int
    compiles cleanly (which surprises me as payload is set but never used)
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        int ch = ' ';
        char file_name[25];
        char payload[3904];
        FILE *fp;
    
        printf("Enter the name of file you wish to see\n");
        fgets(file_name, sizeof(file_name), stdin);
    
        // remove trailing newline
        char *newline = strstr(file_name, "\n" );
        if (newline )  *newline = '\0';
    
        fp = fopen(file_name, "r"); // read mode
        if (fp == NULL)
        {
            perror("Error while opening the file.\n");
            exit(EXIT_FAILURE);
        }
    
        // implied else, fopen successful
    
        printf("The contents of %s file are :\n", file_name);
    
        size_t gin = 0;
        while ( ((ch = fgetc(fp)!=EOF)) && (sizeof(payload) > gin) )
        {
            printf("%ld) %x \n",gin, ch);
            payload[gin++] = ch;
        }
    
        printf("Also, value of gin is %ld --->", gin);
        getchar();
    
        //...rest of the code
        return(0);
    }
    
    0 讨论(0)
提交回复
热议问题