3

I have a console-mode program running on Windows. The program calls getchar() in a loop unitl either an EOF or a 0 is returned. I'd like to enter one of the following as a test vector while running the debugger:

"abc\0" or "abc\EOF

I can't seem to consistently generate either. I tried the suggestion in this question by typing a bcCTRL-ZENTER". That returns 97,98,99,26 to my program, and then hangs on the next getchar() call.

Entering CTRL-D doesn't hlep either, getchar returns a 4 for the control char, then a newline char, and then it duplicates the line I just entered on the console. It's like VS is using the control characters as editing characters.

EDIT:
Here is a stripped down version of the code I am using. I see identical behavior in the debug console and in a command window.

#define MAXSZ 4096
int main(int argc, char **argv)
{
   short int data[MAXSZ]={0}, i=0; 
   char c;
   do {
      if (i==MAXSZ) break;
      c = getchar();
      if (c!=EOF) data[i]=c;
   } while (data[i++]!=0);
   for (i=0;data[i]&&i<MAXSZ;i++)
      putchar(data[i]);
}

How do I enter an EOF or an ASCII 0 in the Visual Studio debug a Windows console?

Community
  • 1
  • 1
AShelly
  • 34,686
  • 15
  • 91
  • 152
  • 1
    A binary zero can be entered by typing Ctrl+@ (hold down Ctrl, press 2 key), echos as ^@ on the screen. You have to press Enter next since getchar() uses buffering. Or use _getch() from `` to input unbuffered text. – Hans Passant Jan 20 '12 at 05:42
  • that's the solution I ended up using for my test. I'd still like to know if it's possible to reliably generate an EOF which gets processed before the Enter key. – AShelly Jan 20 '12 at 19:21

2 Answers2

2

Try this one:

<Enter><Ctrl-Z><Enter>.
CSturgess
  • 1,547
  • 2
  • 13
  • 29
0

@Hans Passant solution works for me - should also work for OP.
1 To generate an ASCII 0, type (Ctrl+@ ) or (Ctrl+2 )
2 To generate an EOF, type (Ctrl+Z Enter), but the input buffer needs to be empty. So this is typically after an (Enter), thus (Enter Ctrl+Z Enter).

But OP code has problems.

char c; // should be int ch
do {
   ...
   c = getchar();
   if (c!=EOF) data[i]=c;
} while (...);

In OP code, if the character ASCII 255 occurs , it gets assigned to a char (-1) which compares to EOF. Instead use int ch.

   if (c!=EOF) data[i]=c;
   // should be
   if (c==EOF) break;
   data[i]=c;

This prevents the code from looping forever or erring once an EOF occurs.


To enter ASCII 255
(Alt key down, num pad 2, num pad 5, num pad 5, Alt key up)

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256