Questions tagged [fgetc]

Anything related to C or C++ standard library functions `fgetc` (C) or `std::fgetc` (C++). These functions are used to read a single character from a stream.

Anything related to C or C++ standard library functions fgetc (defined in <stdio.h> C standard header) or std::fgetc (defined in <cstdio> C++ standard header). These functions are used to read a single character from a stream.

See CPPreference.com:

294 questions
55
votes
3 answers

getc() vs fgetc() - What are the major differences?

Everywhere I see "it is practically identical", or something similar... From The GNU C Programming Tutorial : There is another function in the GNU C Library called fgetc. It is identical to getc in most respects, except that getc is usually…
Joe DF
  • 5,438
  • 6
  • 41
  • 63
13
votes
5 answers

C fgets versus fgetc for reading line

I need to read a line of text (terminated by a newline) without making assumptions about the length. So I now face to possibilities: Use fgets and check each time if the last character is a newline and continuously append to a buffer Read each…
nc3b
  • 15,562
  • 5
  • 51
  • 63
13
votes
5 answers

Confusion with == EOF vs feof

I opened a file, the stream is found at the address of pointer ptr. I am attempting to see whether or not a file is blank. Using the following if (fgetc(ptr) != EOF) works as expected. When the file is blank, the statement is not executed. When the…
user2587754
13
votes
2 answers

fgetc, checking EOF

In the book Linux System Programming I have read some like this: fgetc returns the character read as an unsigned char cast to an int or EOF on end of file or error. A common error using fgetc is: char c; if ((c = fgetc()) != EOF) {...} The right…
pproger
  • 347
  • 1
  • 2
  • 9
10
votes
4 answers

*Might* an unsigned char be equal to EOF?

When using fgetc to read the next character of a stream, you usually check that the end-of-file was not attained by if ((c = fgetc (stream)) != EOF) where c is of int type. Then, either the end-of-file has been attained and the condition will fail,…
Rémi Peyre
  • 410
  • 3
  • 12
9
votes
4 answers

Reading string by char till end of line C/C++

How to read a string one char at the time, and stop when you reach end of line? I'am using fgetc function to read from file and put chars to array (latter will change array to malloc), but can't figure out how to stop when the end of line is…
user3102621
  • 425
  • 2
  • 4
  • 9
8
votes
6 answers

Comparing unsigned char and EOF

when the following code is compiled it goes into an infinite loop: int main() { unsigned char ch; FILE *fp; fp = fopen("abc","r"); if(fp==NULL) { printf("Unable to Open"); exit(1); } while((ch =…
Amol Sharma
  • 1,521
  • 7
  • 20
  • 40
8
votes
3 answers

What are all the reasons `fgetc()` might return `EOF`?

Certainly fgetc() returns EOF when end-of-file or an input error occurs. Is that all and does that mean no more data is available? FILE *inf = ...; int ch; while ((ch = fgetc(inf)) != EOF) { ; } if (feof(inf)) puts("End-of-file"); else if…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
8
votes
3 answers

What is the better way to check EOF and error of fgetc()?

I always use this approach int c; while ((c = fgetc(fp))!=EOF) { printf("%c", c); } As it seems to me more readable and robust. But to an answer of mine link, chux commented that if ( feof(fp) ) is more robust than int c; while ((c =…
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
6
votes
1 answer

fgetc does not identify EOF

The program below runs fine on various Solaris/Linux flavours, but not on AIX. However, if I replace while(c!=EOF) with while(c!=0xff) on AIX it runs completely fine. Any thoughts? I checked the fgetc man page on AIX, and it should return the EOF…
raxit
  • 61
  • 1
  • 2
6
votes
2 answers

Can i use fgetc() or fputc() in a binary file?

I am creating an archive program in C, and i want it to save files i provide, list and extract them. I had many issues because i used a text file for saving, and it is not the best choice if i want to process binary files like music or photos,…
Andrea Gottardi
  • 357
  • 2
  • 3
  • 21
6
votes
4 answers

fgetc(stdin) in a loop is producing strange behaviour

I have this code while(1){ printf("hello world !\n"); fgetc(stdin); } when this runs and I enter a letter like this: hello world ! a it ignores the fgetc(stdin) in the next loop and prints hello world twice without waiting for input. hello…
lilroo
  • 2,928
  • 7
  • 25
  • 34
5
votes
3 answers

Why my inner loop only run once?

a.c #include int main(int argc, char *argv[]) { int i, counter=0; char c; FILE *file=fopen("a.txt","r"); for (i = 0x41 ; i < 0x45; i++) { printf("%c(%x) ",i ,i); while ((c = fgetc(file)) != EOF) …
Pink Panda
  • 59
  • 1
5
votes
3 answers

Is it possible to confuse EOF with a normal byte value when using fgetc?

We often use fgetc like this: int c; while ((c = fgetc(file)) != EOF) { // do stuff } Theoretically, if a byte in the file has the value of EOF, this code is buggy - it will break the loop early and fail to process the whole file. Is this…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
4
votes
4 answers

fgetc null terminator

I'm doing an exercise in K&R: Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. And this is what I have so far (w/o error checking on the file): #include #define tab…
mwlow
  • 141
  • 10
1
2 3
19 20