1

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:

  1. What exactly happens when I send EOF to my kernel.
  2. 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.
Vaibhav Bhavsar
  • 432
  • 4
  • 12
vburns
  • 11
  • 3
  • 3
    You should be calling `fflush(stdout)` _after_ the `printf`, not before it. – Jesin Nov 27 '17 at 03:14
  • 1
    @iBug Only if `stdout` is a console. – Jesin Nov 27 '17 at 03:16
  • In general , outputting a newline does not cause a flush – M.M Nov 27 '17 at 03:53
  • 1
    Pressing Ctrl-Z in a unix-like shell (such as you are using) suspends the process. (It does not kill the program as you seem to be assuming). You can resume the process by typing `fg` (short for "foreground" - bring the suspended job to the foreground). To send EOF press Ctrl-D – M.M Nov 27 '17 at 03:54
  • @iBug Re: [a trailing newline. It serves as fflush](https://stackoverflow.com/questions/47503441/reading-eof-from-getchar-in-c-on-a-windows-10-system#comment81962722_47503441) `'\n'` is not specified to flush the stream. [What are the rules of automatic flushing stdout buffer in C?](https://stackoverflow.com/q/39536212/2410359) – chux - Reinstate Monica Nov 27 '17 at 04:08
  • @vburns Be sure to use `int c` for this task and not `char c`. – chux - Reinstate Monica Nov 27 '17 at 04:11
  • @M.M You need to press `C-D` on an empty line to send an EOF. Same for `C-Z`. Interestingly in Windows CMD you need to press `C-Z` the `Enter` for EOF. – iBug Nov 27 '17 at 04:13
  • You will definitely want to look at [CTRL+Z does not generate EOF in Windows 10](http://proc-x.com/2017/06/ctrlz-does-not-generate-eof-in-windows-10/) – David C. Rankin Nov 27 '17 at 07:09
  • @DavidC.Rankin Nope. It's MSYS, a Unix shell environment for Windows. – iBug Nov 28 '17 at 03:12
  • Then you should be good. It shouldn't matter for msys (though I will have to confirm the next time I boot win10 -- after rolling back the problematic FCU, I don't really have anything to do there now...) – David C. Rankin Nov 28 '17 at 05:39

1 Answers1

1

What exactly happens when i send EOF to my kernel.

The system (MSYS) tells the program that its standard input has reached EOF (end-of-file), and there'll be no more input availablr.

Is there a way to send EOF without immediately killing my program?

No. Sending an EOF never terminates a program immediately. It lets the program continue without reading input anymore. It's your program that terminates itself when it found nothing to read.

The reason you see Stopped is that you didn't send an EOF at all! Oj Unix shells (including MSYS and Cygwin), Ctrl-Z means Suspend. You can type fg to bring it to foreground again, then press Ctrl-D to send it an EOF.

Note: Ctrl-Z sends EOF only in Windows CMD, and in Windows 10 you have to uncheck a box.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • Note for the Win10 [CTRL+Z does not generate EOF in Windows 10](http://proc-x.com/2017/06/ctrlz-does-not-generate-eof-in-windows-10/) .. until you check the right box. – David C. Rankin Nov 27 '17 at 07:10
  • @DavidC.Rankin OMG. Then how to send EOF without checking that box? – iBug Nov 27 '17 at 07:16
  • You can't. Leave it to M$ to forget all about the need to generate an `EOF` from the command line `:(` You can't even generate a `EOF` in the VS Developers Console until you check that box... – David C. Rankin Nov 27 '17 at 07:24
  • @DavudC.Rankin Thank God I've been using WSL since Windows 10. I'm adapted to Ctrl-D now :) – iBug Nov 27 '17 at 07:31
  • @iBug First of all thank you for your answer. So when you said "No. Sending an EOF never terminates a program immediately. It lets the program continue without reading input anymore. It's your program that terminates itself when it found nothing to read." I don't understand why program killed itself before seeing the printf("%ld\n",(counter));. It should've stayed up until we return correct? – vburns Nov 27 '17 at 20:05