整个文件不在C中读取(EOF意外发生) [英] Entire file is not read in C (EOF occurs unexpectedly)

查看:251
本文介绍了整个文件不在C中读取(EOF意外发生)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打印大约4000个字符的文件的内容。
不知何故,程序只记录前220个字符并终止。

  int main(void)
{
char ch ='',file_name [25],payload [3904];
FILE * fp;
printf(输入你希望看到的文件名称\\\
);
获取(file_name);
fp = fopen(file_name,r); //读取模式
if(fp == NULL)
{
perror(Error when open the file.\\\
);
exit(EXIT_FAILURE);


printf(%s文件的内容是:\ n,file_name);

int gin = 0; $(b)
while((ch = fgetc
有效载荷[gin ++] = ch;
}
printf(同样,杜松子酒的价值是%d --->,gin);
getchar();
$ b $ //...rest of the code
}

gin 的值是220.



为了检查,我修改了 while()条件运行文件中的确切字符数:

  {
// ...

while(gin< 3904)
{
if((ch = fgetc(fp))== EOF)res ++; // ADDED THIS计算EOF的
printf(%d)%x \\\
,gin,ch);
有效载荷[gin ++] = ch;
// printf(%x \\\
,payload [(gin - 1)]);

if(gin%100 == 0)
{
printf(同样,res的值是%d --->,res);的getchar();
getchar();
}
}

//...rest of the code
}

杜松子酒的值达到3904,res的值(EOF的号码)是3684,这意味着前220个字符后面的每个字符都被读为EOF。程序从第一个220个字符开始读取 FF ,即使它已经被填充了。

解决方案

我认为代码是好的,除了你应该改变 ch int

fgetc( ) 返回




  • 如果成功, int

  • 失败时, EOF

    所以,首先,你必须改变<$ c $因为从 fgetc()的一些返回值可能不是 int em> a char



    现在,在第二种情况下,检查 fgetc() EOF 的返回值来检测任何错误。你只需要返回值,并试图将这些值存储到数组中。实际上,到达文件末尾时,没有什么可读的了,所有在同一个文件指针上的进一步读取都会返回错误信息。



    最有可能的是那些价值。之后 220 在你的情况下是有效的,根本就不是。

    在你的问题中的声明,

    lockquote

    (EOF意外发生)




    是错误的。它发生得很好,你忽略它,遇到麻烦。




    注意:
    $ b


    1. 在你的第一个片段中,你要做两个连续的 fgetc()调用,基本上放弃第一个调用的结果,并使用第二个调用,而不进行任何检查。
    2. 不要使用 gets()。它遭受缓冲区溢出问题。始终使用 fgets()


    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(void)
    {
        char ch = ' ', file_name[25], payload[3904];
        FILE *fp;
        printf("Enter the name of file you wish to see\n");
        gets(file_name);
        fp = fopen(file_name, "r"); // read mode
        if (fp == NULL)
        {
            perror("Error while opening the file.\n");
            exit(EXIT_FAILURE);
        }
    
        printf("The contents of %s file are :\n", file_name);
    
        int gin = 0;
        while ((ch = fgetc(fp)!=EOF))
        {
            printf("%d) %x \n",gin, ch);
            payload[gin++] = ch;
        }
        printf("Also, value of gin is %d --->", gin);
        getchar();
    
        //...rest of the code
    }
    

    Here the value of gin is 220.

    Just to check, I modified the while() condition to run for the exact number of characters in the file:

    {
     //...
    
     while (gin<3904)
     {
        if ((ch = fgetc(fp)) == EOF) res++;//ADDED THIS TO COUNT NUMBER OF EOF's
        printf("%d) %x \n",gin, ch);
        payload[gin++] = ch;
        //printf(" %x \n", payload[(gin - 1)]);
    
        if (gin % 100 == 0)     
        {
            printf("Also, value of res is %d --->", res); getchar();
            getchar();
        }
     }
    
     //...rest of the code
    }
    

    The value of gin reaches 3904, the value of res(no. of EOF's) is 3684, meaning that every character after the first 220 is being read as an EOF. The program starts reading FF after the first 220 character even though it is filled.

    解决方案

    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.

    这篇关于整个文件不在C中读取(EOF意外发生)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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