I'm working on character K&R C book to try to learn the C language. I'm having a lot of issues because I'm using windows 10 OS instead of Linux. I'm using msys2 to compile and run my code. I for the longest time couldn't figure out the File copying from section 1.5.1 until asking for help from an expert in the field. He pointed out that I needed to use fflush(stdout);
{
while ((c = getchar()) != EOF) { // read buffer store in c, then check if it is EOF(ctrl+z on windows)
fflush(stdout); //flush buffer
putchar(c); // print the character retrieved
printf("%d\n",(c));
}
return(0);
}
Adding that made my program work as expected. However, Now I'm running into similar difficulty on the character counter of the next section.
long counter = 0; // initialize c variable
while (getchar() != EOF) { //check if character received is EOF(ctrl+z on windows)
fflush(stdout); //flush buffer
++counter; // incrementcounter
}
fflush(stdout);
printf("%ld\n",(counter));
return(0);
I never see the print on my counter at the end. The attached image shows what happens when I run the file. The "Stopped" print is from when I sent EOF(ctr+Z).
I essentially have two questions:
- What exactly happens when I send EOF to my kernel.
- Is there a way to send EOF without immediately killing my program? I hope that was easy to follow and thank you all in advance.