为什么 fgetc() 返回 int 而不是 char? [英] Why does fgetc() return int instead of char?

查看:28
本文介绍了为什么 fgetc() 返回 int 而不是 char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将二进制文件源复制到文件目标.而已!该代码的灵感来自互联网上的许多示例.

#include int main(int argc, char **argv) {文件 *fp1,*fp2;字符 ch;fp1 = fopen("source.pdf", "r");fp2 = fopen("target.pdf", "w");while((ch = fgetc(fp1)) != EOF)fputc(ch, fp2);fclose(fp1);fclose(fp2);返回0;}

结果因文件大小而异.

root@vm:/home/coder/test# ls -l-rwxr-x--- 1 root root 14593 Feb 28 10:24 source.pdf-rw-r--r-- 1 根根 159 Mar 1 20:19 target.pdf

好的,有什么问题吗?

我知道 char 是无符号的,并且在 80 以上时会被签名.请参阅此处.

这在我使用 printf("%x\n", ch); 时得到证实,它返回大约 50% 的时间,有时类似于 FFFFFFE1.>

我的问题的解决方案是使用 int i.s.o.char.

使用 char 找到的示例:示例 1, 示例 2示例 3示例 4, ...

使用 int 找到的示例:示例 a,...

我不使用花哨的编译器选项.

为什么发现几乎所有代码示例都将 fgetc() 返回到 char i.s.o.int,哪个更正确?

我错过了什么?

解决方案

ISO C 要求 fgetc() 返回一个 int 因为它必须能够返回所有可能的字符除了文件结束指示符.

因此,将返回值放入 char 使用它来检测 EOF 的代码通常是完全错误的,不应使用.

<小时>

话虽如此,两个你给出的例子实际上并没有这样做.

其中一个使用 fseekftell 来获取文件中的字节数,然后使用 that 来控制读/写环形.这可能是有问题的,因为在检索大小之后,文件实际上可以改变大小,但这与尝试将 int 强制转换为 char.

另一个在读取字符后立即使用 feof 来检查是否已到达文件末尾.

<小时>

但是您说得对,最简单的方法就是正确使用返回值,例如:

int charInt;while ((charInt = fgetc(inputHandle)) != EOF)doSomethingWith(charInt);

I would like to copy binary file source to file target. Nothing more! The code is inspired from many examples found on the Internet.

#include <stdio.h>

int main(int argc, char **argv) {

    FILE *fp1, *fp2;
    char ch;

    fp1 = fopen("source.pdf", "r");
    fp2 = fopen("target.pdf", "w");

    while((ch = fgetc(fp1)) != EOF)
        fputc(ch, fp2);

    fclose(fp1);
    fclose(fp2);

    return 0;

}

The result differs in file size.

root@vm:/home/coder/test# ls -l
-rwxr-x--- 1 root root 14593 Feb 28 10:24 source.pdf
-rw-r--r-- 1 root root   159 Mar  1 20:19 target.pdf

Ok, so what's the problem?

I know that char is unsigned and get signed when above 80. See here.

This is confirmed when I use printf("%x\n", ch); which returns approximately 50% of the time something like sometimes FFFFFFE1.

The solution to the my issue would be to use int i.s.o. char.

Examples found with char: example 1, example 2 example 3, example 4, ...

Examples found with int: example a, ...

I don't use fancy compiler options.

Why are virtually all code examples found returning fgetc() to an char i.s.o. an int, which would be more correct?

What am I missing?

解决方案

ISO C mandates that fgetc() returns an int since it must be able to return every possible character in addition to an end-of-file indicator.

So code that places the return value into a char, and uses it to detect EOF, is generally plain wrong and should not be used.


Having said that, two of the examples you gave don't actually do that.

One of them uses fseek and ftell to get the number of bytes in the file and then uses that to control the read/write loop. That's could be problematic since the file can actually change in size after the size is retrieved but that's a different problem to trying to force an int into a char.

The other uses feof immediately after the character is read to check if the end of file has been reached.


But you're correct in that the easiest way to do it is to simply use the return value correctly, something like:

int charInt;
while ((charInt = fgetc(inputHandle)) != EOF)
    doSomethingWith(charInt);

这篇关于为什么 fgetc() 返回 int 而不是 char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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