This code mix calls to putc() and ungetc() on the same file:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
int c;
fp = fopen("filetest.txt", "r+");
int fno = fileno(fp);
/* get and print first position '1' */
if((c = fgetc(fp)) == EOF){
printf("fgetc(): error\n");
}else{
printf("%c\n", c);
}
/* get 2nd position '2' */
if((c = fgetc(fp)) == EOF){
printf("fgetc(): error\n");
}
/* put char 'X' in the 3rd position*/
if((fputc('X', fp)) == EOF){
printf("fputc(): error\n");
}
/* -- problematic code --- */
/* Unget '2' */
if (ungetc(c, fp) == EOF){
printf("ungetc(): error\n");
}
/* get again and print '2' */
if((c = fgetc(fp)) == EOF){
printf("fgetc(): error\n");
}else{
printf("%c\n", c);
}
/* -- end of problematic code --- */
/* get 4rd position '4' */
if((c = fgetc(fp)) == EOF){
printf("fgetc(): error\n");
}else{
printf("%c\n", c);
}
if (fclose(fp) != 0){
printf("fclose(): error\n");
}
}
Being the contents of the 'filetest.txt':
12345
I thought the output would be:
1
2
4
but instead I got an error in fgetc():
1
2
fgetc(): error
fclose(): error
If the part that says "problematic code" is removed, the output is ok:
1
4
why is it failing?
Since stackoverflow doesn't allow me to post it (too much code and too little text explanations) here is some filler text of what I have tried:
- reading the man pages for getc, fgetc, fputc, and ungetc
- originally I was using putc, getc so I changed to fputc and fputc to discard macro errors (I was just desesperate)
The next is to read the implementation of getc, ungetc and putc but I was wondering if someone could help explain me this before that...