I build a small program that simply copy the text from an input.txt
file to an output.txt
file. It works with no apparent problem with the command:
./myCopier.txt < rand.txt > randout.txt
on a GCC89 compiler, used for didactic purposes.
#include<stdio.h>
int main(void){
int c;
while ((c = getchar()) != EOF)
putchar(c);
/*The text in printf() does not appear anywhere*/
printf("My theories and toughts!\n");
}
Later, I add printf()
in order to print my thoughts about how the program worked. It compiles without errors or warnings even with restrictive parameters (-Wall
), but this text does not appear in any place. By any place, I mean both output.txt
and the standard output.
I am thinking that:
- The command
./myCopier.exe
alone clearly create an endless loop. Without text input there is noEOL
character, hence theprint()
command is never reached. - So, why when the input is provided, the
printf()
command has no apparent effect? I'd expect the text fromprintf()
to appear on the standard output, just when the loop closes andoutput.txt
is created. Why does it not occur? - My theory is that
./myCopier.txt < rand.txt > randout.txt
does not allow it. For some reason, it makes the program only transfer the input to the output, "ignoring" everything does not come from the input. Am I correct? If so, why?
If you are interested in the problem with deeper detail, here is the assembly code: