Microsoft KB Archive/57948: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - ">" to ">")
 
Line 103: Line 103:
*/  
*/  


#include <conio.h&gt;
#include <conio.h>
#include <stdio.h&gt;
#include <stdio.h>


char buf[22];
char buf[22];
Line 125: Line 125:
                 </pre>
                 </pre>
'''Note''' You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
'''Note''' You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
# Click '''Project''', and then click '''<code><ProjectName&gt;</code> Properties'''.<br />
# Click '''Project''', and then click '''<code><ProjectName></code> Properties'''.<br />
<br />
<br />
'''Note''' <code><ProjectName&gt;</code> is a placeholder for the name of the project.
'''Note''' <code><ProjectName></code> is a placeholder for the name of the project.
# Expand '''Configuration Properties''', and then click '''General'''.
# Expand '''Configuration Properties''', and then click '''General'''.
# Click to select '''Common Language Runtime Support, Old Syntax (/clr:oldSyntax)''' in the '''Common Language Runtime support''' project setting in the right pane, click '''Apply''', and then click '''OK'''.
# Click to select '''Common Language Runtime Support, Old Syntax (/clr:oldSyntax)''' in the '''Common Language Runtime support''' project setting in the right pane, click '''Apply''', and then click '''OK'''.
Line 142: Line 142:
<br />
<br />
Enter the following string as a test:
Enter the following string as a test:
<pre class="fixed_text">  abcdef<esc&gt;ghijk
<pre class="fixed_text">  abcdef<esc>ghijk
                 </pre>
                 </pre>
Note that the resulting string is output as:
Note that the resulting string is output as:
<pre class="fixed_text">  ghijk
<pre class="fixed_text">  ghijk
                 </pre>
                 </pre>
Now, create a data file named test.dat containing the string &quot;abcdef<esc&gt;ghijk&quot; either by using a text editor that permits escape characters to be embedded in a string or by using the sample code #2 below.
Now, create a data file named test.dat containing the string &quot;abcdef<esc>ghijk&quot; either by using a text editor that permits escape characters to be embedded in a string or by using the sample code #2 below.
=== Sample Code #2 ===
=== Sample Code #2 ===


Line 153: Line 153:
*/  
*/  


#include <stdio.h&gt;
#include <stdio.h>


void main(void)
void main(void)
Line 175: Line 175:
                 </pre>
                 </pre>
the resulting string is output as follows:
the resulting string is output as follows:
<pre class="fixed_text">  abcdef<esc&gt;ghijk
<pre class="fixed_text">  abcdef<esc>ghijk
                 </pre>
                 </pre>
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, OS/2, or Windows NT 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.
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, OS/2, or Windows NT 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.

Latest revision as of 10:19, 21 July 2020

Knowledge Base


Article ID: 57948

Article Last Modified on 1/11/2006



APPLIES TO

  • The C Run-Time (CRT), when used with:
    • Microsoft C Professional Development System 6.0a
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 5.0 Standard Edition
    • Microsoft Visual C++ 6.0 Service Pack 5
    • Microsoft Visual C++ 2005 Express Edition
    • Microsoft Visual C++ .NET 2003 Standard Edition
    • Microsoft Visual C++ .NET 2002 Standard Edition



This article was previously published under Q57948


Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SUMMARY

In Microsoft C for MS-DOS and Windows NT, 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 #1

/* 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]);
     }
}
                

Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:

  1. Click Project, and then click <ProjectName> Properties.


Note <ProjectName> is a placeholder for the name of the project.

  1. Expand Configuration Properties, and then click General.
  2. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx


These steps apply to the whole article.

Enter the following string as a test:

   abcdef<esc>ghijk
                

Note that the resulting string is output as:

   ghijk
                

Now, create a data file named test.dat containing the string "abcdef<esc>ghijk" either by using a text editor that permits escape characters to be embedded in a string or by using the sample code #2 below.

Sample Code #2

/* Compile options needed: none
*/ 

#include <stdio.h>

void main(void)
{
   FILE* fp;
   char* fname = "test.dat";
   int   numwrote;

   if ( (fp = fopen( fname, "wt" )) == NULL )
      printf( "Unable to open text file \"%s\" for writing.\n", fname );
   else
   {
      if ( (numwrote = fprintf( fp, "abcdef""\x1b""ghijk" )) != 12 )
         printf( "Write to file failed!  %d bytes written!\n", numwrote );
      fclose( fp );
   }
}
                

If the program from sample code #1 is run with input redirected from the data file, as follows:

   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, OS/2, or Windows NT 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.

Keywords: kbinfo kbcode KB57948