My application is built with Eclipse CDT Neon (4.6.2RC3) and gcc (Debian 6.3.0-18) 6.3.0 20170516
#include <stdio.h>
#include <stdlib.h>
int
main()
{
for (int i = 0;;i++) { /* Loop through characters. */
int ch;
ch = getc(stdin); /* Read the next character. */
if (i < 100 || i > 67730) fprintf(stderr, "%i(%i) ", i, ch);
else if (i == 100) fprintf(stderr, " ----- ");
if (ch == EOF)
break; /* Exit loop on end-of-file. */
}
fprintf(stderr, "\n\n*** finished successfully ***\n");
return 0;
}
Here are my compilation options: -O0 -pedantic -Wall -Wextra -c -ggdb -fmessage-length=0 -Wno-implicit-function-declaration
. I run it with help of an input file piped to stdin
.
When I run it from the console, everything is fine. Here is the end of the normal output when runned from the console:
./myprogram < inputfile.pdf
67825(32) 67826(54) 67827(10) 67828(10) 67829(12) 67830(-1)
*** finished successfully ***
But when I run it in debug mode from Eclipse CDT, it waits indefinitely at the last character (67829th) before EOF (67830th) and the program never exits. If I suspend execution, I can check it is stucked inside the ch = getc(stdin);
line. Here is the stack at this point :
Thread #1 [ArithmeticCoder] 29598 [core: 5] (Running : User Request)
Thread #1 [myprogram] 29598 [core: 6] (Suspended : Signal : SIGINT:Interrupt)
__read_nocancel() at /build/glibc-p3Km7c/glibc-2.24/io/../sysdeps/unix/syscall-template.S:84 0x7ffff7811700
_IO_new_file_underflow() at /build/glibc-p3Km7c/glibc-2.24/libio/fileops.c:600 0x7ffff77a9a00
__GI__IO_default_uflow() at /build/glibc-p3Km7c/glibc-2.24/libio/genops.c:413 0x7ffff77aab02
_IO_getc() at /build/glibc-p3Km7c/glibc-2.24/libio/getc.c:38 0x7ffff77a54f0
main() at /.../WorkSpace/myprogram/src/myprogram.c:20 0x555555554f08
I have tried:
- with
fgetc
which is very similar, but the result is the same. - in Eclipse CDT RUN mode, and it does not reach the end of the file. I assume the problem is the same and due to running this application under the Eclipse CDT environment.
- Replacing
ch = getc(stdin); if (ch == EOF)
byint s = fread (&ch, 1, 1, stdin ); if (ch == EOF || s == 0)
, the result is the same ! I expected not since their behaviour is really different.
What kind of mistake may I have in my settings?
Any idea please ?