What is the difference between fscanf and fgetc? I have a few people arguing with me on the differences. I thought fgetc reads character by character of a file. I thought fscanf reads the whole file. Is that corrrect? Can someone elaborate on what I forgot?

Is that corrrect?

Not at all. Have you checked a C reference manual? The only similarities fgetc and fscanf have is they read input from a stream, and fscanf can simulate fgetc.

The people you were arguing with, what were their points? Perhaps we can clear up any errors on both sides if you elaborate.

My understanding of fgetc is it reads character by character of a file. They think fscanf can also read character by character of a file. I do not think that is correct. I think fscanf is used for reading an entire file. I may not be explaing that with the correct terminology but I have used it for reading an entire file before.

My understanding of fgetc is it reads character by character of a file

It reads one character from a stream. The stream may or may not be a stream to a file. For example, fgetc(stdin) is perfectly acceptable.

They think fscanf can also read character by character of a file. I do not think that is correct.

It can. The call that's equivalent to fgetc is fscanf(stream, "%c", &myChar).

I think fscanf is used for reading an entire file.

No.

but I have used it for reading an entire file before

It's possible with a single call, but very dependent on the format of the file. Most of the time you read lines from a file in a loop rather than everything.

Thank you :). Were both saying the same thing but can't explain it in a way the other understands.

One small subtle point.

fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

Edited 3 Years Ago by gerard4143

And if you need a char you always cast it like this :).

while ((c = fgetc(pFile)) != EOF)
{
    vertices[addcounter] = (char)c;
}
This article has been dead for over six months. Start a new discussion instead.