Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

c - Why does fgetc() return int instead of char?

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 ", 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...