INF: Escape Character Erases First Characters in gets() String

ID Number: Q57948

5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, if you enter the escape character (ASCII 1Bh) from the keyboard

(console) into a string that gets(), cgets(), or fgets() is reading, all

the string previously read in is erased. The string pointer is reset so

that characters following the escape character are read into the

beginning of the string. This is consistent with the action taken by the

operating system to parse the input line.

However, if the escape character is input from a file by redirection,

the entire string, including the escape character, will be read into

the string.

More Information:

Sample Code

-----------

/* Compile options needed: none

*/

#include <conio.h>

#include <stdio.h>

char buf[22];

char *result;

void main(void)

{

int i;

buf[0] = 20;

printf("Enter your text: \n");

result = gets(buf);

printf ( "Resulting String: %s\n", result );

for( i = 0; i < 20; i ++ )

{

printf("Buf[%2d] = %c (char)\n", i, buf[i]);

}

}

Enter the following string as a test:

abcdef<esc>ghijk

Note that the resulting string is output as:

ghijk

Now, using a text editor that will accept an escape character embedded

in a string, create a data file with the same string. If the above

program is run with input redirected from the data file, for example

program <test.dat

the resulting string is output as follows:

abcdef<esc>ghijk

This behavior occurs in the entire gets() family of routines,

including gets(), cgets(), and fgets(). If the input is coming from

the console, the run time will use the standard MS-DOS and OS/2

keyboard read routines. On the other hand, if the input is coming from

a file (through redirection), the operating system doesn't perform any

editing and the file is read in literally.

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00