Ending a loop with EOF (no input)

I'm currently trying to end the while loop with something like this:

#include <stdio.h>
int main() 
{
    while(getchar() != EOF)
    {
        if( getchar() == EOF )
            break;
    }
    return 0;

}

When I click CTRL+Don my Ubuntu, it immediately terminates the loop. But on Windows, I have to click CTRL+Z, and then click ENTERto close the loop. Can I get rid of ENTERin Windows?

+4
source share
2 answers

Getchar behavior

linux EOF char ctrl + d, Windows enter CRT ctrl + z ( ). , . , , EOF char getchar enter, ctrl + z.

:

Microsoft DOS Windows ( CP/M DEC) EOF. , ( " " ) ; ASCII Control-Z. 26. MS-DOS, Microsoft MS-DOS (COMMAND.COM) (, EDLIN), Control-Z , , / Control-Z . :

  • CP/M. CP/M 128- "", Control-Z , . MS-DOS , MS-DOS.

  • , .

​​:

(, CSV-1203 [6]) - EOF . , Control + Z EOF MS-DOS Microsoft Windows, API- .

(, Visual Basic) "" EOF , (INPUT, LINE INPUT ..), , . .

26 " ", ASCII Substitute, .

:

#include <stdio.h>

int main() {
  while(1) {
    char c = getchar();
    printf("%d\n", c); 
    if (c == EOF)      // tried with also -1 and 26
      break;
  }
  return 0;
}

, Windows , EOF (-1) , enter. - , a ^Z ( ). , :

  • Microsoft Compiler
  • GCC
  • CMD
  • bash windows

API Windows

@eryksun, ( ) Windows, conhost, " ctrl + d". , . IMHO, , 0. , , , stdin , .

:

  • , , , conhost (, , ..).
  • , ( )
  • EOF ( 4 , ) ascii.

:

#include <windows.h>
#include <stdio.h>

#define Kev input_buffer[i].Event.KeyEvent // a shortcut

int main(void) {
  HANDLE h_std_in;                // Handler for the stdin
  DWORD read_count,               // number of events intercepted by ReadConsoleInput
        i;                        // iterator
  INPUT_RECORD input_buffer[128]; // Vector of events

  h_std_in = GetStdHandle( // Get the stdin handler
    STD_INPUT_HANDLE       // enumerator for stdin. Others exist for stdout and stderr
  ); 

  while(1) {
    ReadConsoleInput( // Read the input from the handler
      h_std_in,       // our handler 
      input_buffer,   // the vector in which events will be saved
      128,            // the dimension of the vector
      &read_count);   // the number of events captured and saved (always < 128 in this case)

    for (i = 0; i < read_count; i++) {    // and here we iterate from 0 to read_count
      switch(input_buffer[i].EventType) { // let check the type of event 
        case KEY_EVENT:                   // to intercept the keyboard ones
          if (Kev.bKeyDown) {             // and refine only on key pressed (avoid a second event for key released)
            // Intercepts CTRL + D
            if (Kev.uChar.AsciiChar != 4)
              printf("%c", Kev.uChar.AsciiChar);
            else
              return 0;
          }
          break;
        default:
          break;
      }
    }
  }

  return 0;
}
+6
    while(getchar() != EOF)
    {
        if( getchar() == EOF )
            break;
    }
    return 0;

.

getchar() != EOF, , ( getchar() == EOF) . , getchar() == EOF .

, getchar() 2 , , 2 1.

?

0

Source: https://habr.com/ru/post/1688202/


All Articles