PS-Trainer C - Entwicklung
Character - StreamIO
Homepage von PS-Trainer - C-Entwicklung - Stream-IO - an PS-Trainer
PS-Trainer PS-Trainer

StreamIO Data Stream Input & Output
Character tables Tables of common character sets
 
getc, getwc, getchar, getwchar Read a character from a stream (getc, getwc), or get a character from stdin (getchar, getwchar).
_getch, _getche
Get a character from the console without echo (_getch) or with echo (_getche).
putc, putwc, putchar, putwchar Writes a character to a stream (putc, putwc) or to stdout (putchar, putwchar).
_putch Writes a character to the console.
_ungetch
Pushes back the last charcter read from the console.


getc, getwc, getchar, getwchar
Read a character from a stream (getc, getwc), or get a character from stdin (getchar, getwchar).
int getc( FILE *stream );
wint_t getwc( FILE *stream );
int getchar( void );
wint_t getwchar( void );


Routine Required Header Compatibility
getc <stdio.h> ANSI, Win 95, Win NT
getwc <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
getchar <stdio.h> ANSI, Win 95, Win NT
getwchar <stdio.h> or <wchar.h> ANSI, Win 95, Win NT

Remarks
Each of these routines reads a single character from a file at the current position and increments the associated file pointer (if defined) to point to the next character. In the case of getc and getwc, the file is associated with stream (see Choosing Between Functions and Macros). Routine-specific remarks follow.
Routine Remarks

getc Same as fgetc, but implemented as a function and as a macro.
getwc Wide-character version of getc. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.
getchar Same as _fgetchar, but implemented as a function and as a macro.
getwchar Wide-character version of getchar. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_gettc getc getc getwc
_gettchar getchar getchar getwchar

Subject: Stream I/O Routines
Keywords: See Also fgetc, _getch, putc, ungetc
Return Value
Each of these functions returns the character read. To indicate an read error or end-of-file condition, getc and getchar return EOF, and getwc and getwchar return WEOF. For getc and getchar, use ferror or feof to check for an error or for end of file.

Parameter
string Input stream

Example
/* GETC.C: This program uses getchar to read a single line
* of input from stdin, places this input in buffer, then
* terminates the string before printing it to the screen.
*/

#include <stdio.h>

void main( void )
{
char buffer[81];
int i, ch;

printf( "Enter a line: " );

/* Read in single line from "stdin": */
for( i = 0; (i < 80) && ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
buffer[i] = (char)ch;

/* Terminate string with null character: */
buffer[i] = '\0';
printf( "%s\n", buffer );
}


Output
Enter a line: This is a test
This is a test


_getch, _getche
Get a character from the console without echo (_getch) or with echo (_getche).
int _getch( void );
int _getche( void );


Routine Required Header Compatibility
_getch <conio.h> Win 95, Win NT
_getche <conio.h> Win 95, Win NT

Remarks
The _getch function reads a single character from the console without echoing.
_getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

Subject: Console and Port I/O Routines
Keywords: See also _cgets, getc, _ungetch
Return Value
Both _getch and _getche return the character read. There is no error return.

Example
/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/

#include <conio.h>
#include <ctype.h>

void main( void )
{
int ch;

_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );

_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}


Output
Type 'Y' when finished typing keys: Y


putc, putwc, putchar, putwchar
Writes a character to a stream (putc, putwc) or to stdout (putchar, putwchar).
int putc( int c, FILE *stream );
wint_t putwc( wint_t c, FILE *stream );
int putchar( int c );
wint_t putwchar( wint_t c );


Routine Required Header Compatibility
putc <stdio.h> ANSI, Win 95, Win NT
putwc <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
putchar <stdio.h> ANSI, Win 95, Win NT
putwchar <stdio.h> or <wchar.h> ANSI, Win 95, Win NT

Remarks
The putc routine writes the single character c to the output stream at the current position. Any integer can be passed to putc, but only the lower 8 bits are written.
The putchar routine is identical to putc( c, stdout ). For each routine, if a read error occurs, the error indicator for the stream is set. putc and putchar are similar to fputc and _fputchar, respectively, but are implemented both as functions and as macros (see Choosing Between Functions and Macros).
putwc and putwchar are wide-character versions of putc and putchar, respectively.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_puttc putc putc putwc
_puttchar putchar putchar putwchar

Subject: Stream I/O Routines
Keywords: See also fputc, getc
Return Value
Each of these functions returns the character written. To indicate an error or end-of-file condition, putc and putchar return EOF; putwc and putwchar return WEOF. For all four routines, use ferror or feof to check for an error or end of file.

Parameters
c Character to be written
stream Pointer to FILE structure
Example
/* PUTC.C: This program uses putc to write buffer
* to a stream. If an error occurs, the program
* stops before writing the entire buffer.
*/

#include <stdio.h>

void main( void )
{
FILE *stream;
char *p, buffer[] = "This is the line of output\n";
int ch;

ch = 0;
/* Make standard out the stream and write to it. */
stream = stdout;
for( p = buffer; (ch != EOF) && (*p != '\0'); p++ )
ch = putc( *p, stream );
}


Output
This is the line of output


_putch
Writes a character to the console.
int _putch( int c );

Routine Required Header Compatibility
putch <conio.h> Win 95, Win NT

Remarks
The _putch function writes the character c directly (without buffering) to the console.

Subject: Console and Port I/O Routines
Keywords: See also _cprintf, _getch
Return Value
The function returns c if successful, and EOF if not.
Parameter
c Character to be output

Example
/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/

#include <conio.h>
#include <ctype.h>

void main( void )
{
int ch;

_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );

_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}


Output
Type 'Y' when finished typing keys: Y


_ungetch
Pushes back the last charcter read from the console.
int _ungetch( int c );

Routine Required Header Compatibility
_ungetch <conio.h> Win 95, Win NT

Remarks
The _ungetch function pushes the character c back to the console, causing c to be the next character read by _getch or _getche. _ungetch fails if it is called more than once before the next read. The c argument may not be EOF.

Subject: Console and Port I/O Routines
Keywords: See also _cscanf, _getch
Return Value
_ungetch returns the character c if it is successful. A return value of EOF indicates an error.

Parameter
c Character to be pushed
Example
/* UNGETCH.C: In this program, a white-space delimited
* token is read from the keyboard. When the program
* encounters a delimiter, it uses _ungetch to replace
* the character in the keyboard buffer.
*/

#include <conio.h>
#include <ctype.h>
#include <stdio.h>

void main( void )
{
char buffer[100];
int count = 0;
int ch;
ch = _getche();
while( isspace( ch ) ) /* Skip preceding white space. */
ch = _getche();
while( count < 99 ) /* Gather token. */
{
if( isspace( ch ) ) /* End of token. */
break;
buffer[count++] = (char)ch;
ch = _getche();
}
_ungetch( ch ); /* Put back delimiter. */
buffer[count] = '\0'; /* Null terminate the token. */
printf( "\ntoken = %s\n", buffer );
}


Output
White
token = White


Homepage von PS-Trainer - C-Entwicklung - Stream-IO - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1