![]() |
C - Entwicklung Bibliotheks-Funktionen: StreamIO Homepage von PS-Trainer - C-Entwicklung - Bibliotheken - an PS-Trainer |
|
| StreamIO | Data Stream Input & Output | ||||||||||||||||||||||||||||||||||||||||||
| Standard streams for input, output, and error output. |
|
||||||||||||||||||||||||||||||||||||||||||
| Standard Formatted input and output |
|
||||||||||||||||||||||||||||||||||||||||||
| Standard Character input and output |
|
||||||||||||||||||||||||||||||||||||||||||
| Standard Line input and output |
|
||||||||||||||||||||||||||||||||||||||||||
| Error messages |
|
||||||||||||||||||||||||||||||||||||||||||
| Console |
|
||||||||||||||||||||||||||||||||||||||||||
| File Handling |
|
||||||||||||||||||||||||||||||||||||||||||
| File open & close |
|
||||||||||||||||||||||||||||||||||||||||||
| File read & write |
|
||||||||||||||||||||||||||||||||||||||||||
| Stream open & close |
|
||||||||||||||||||||||||||||||||||||||||||
| Stream read & write |
|
||||||||||||||||||||||||||||||||||||||||||
| Stream I/O These functions process data in different sizes and formats, from single characters to large data structures. They also provide buffering, which can improve performance. The default size of a stream buffer is 4K. These routines affect only buffers created by the run-time library routines, and have no effect on buffers created by the operating system. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| When a program begins execution, the startup code automatically opens
several streams: standard input (pointed to by stdin),
standard output (pointed to by stdout),
and standard error (pointed to by stderr).
These streams are directed to the console (keyboard and screen)
by default. Use freopen
to redirect stdin, stdout,
or stderr to a disk file
or a device. Files opened using the stream routines are buffered by default. The stdout and stderr functions are flushed whenever they are full or, if you are writing to a character device, after each library call. If a program terminates abnormally, output buffers may not be flushed, resulting in loss of data. Use fflush or _flushall to ensure that the buffer associated with a specified file or all open buffers are flushed to the operating system, which can cache data before writing it to disk. The commit-to-disk feature ensures that the flushed buffer contents are not lost in the event of a system failure. There are two ways to commit buffer contents to disk: Link with the file COMMODE.OBJ to set a global commit flag. The default setting of the global flag is n, for "no-commit." Set the mode flag to c with fopen or _fdopen. Any file specifically opened with either the c or the n flag behaves according to the flag, regardless of the state of the global commit/no-commit flag. If your program does not explicitly close a stream, the stream is automatically closed when the program terminates. However, you should close a stream when your program finishes with it, as the number of streams that can be open at one time is limited. See _setmaxstdio for information on this limit. Input can follow output directly only with an intervening call to fflush or to a file-positioning function (fseek, fsetpos, or rewind). Output can follow input without an intervening call to a file-positioning function if the input operation encounters the end of the file. |
| _getw Gets an integer from a stream. int _getw( FILE *stream );
Remarks The _getw function reads the next binary value of type int from the file associated with stream and increments the associated file pointer (if there is one) to point to the next unread character. _getw does not assume any special alignment of items in the stream. Problems with porting may occur with _getw because the size of the int type and the ordering of bytes within the int type differ across systems. Subject: Stream I/O Routines Keywords: See also _putw |
||||||
| Return Value _getw returns the integer value read. A return value of EOF indicates either an error or end of file. However, because the EOF value is also a legitimate integer value, use feof or ferror to verify an end-of-file or error condition. Parameter
|
||||||
| Example /* GETW.C: This program uses _getw to read a word * from a stream, then performs an error check. */ #include <stdio.h> void main( void ) if( (stream = fopen( "getw.c",
"rb" )) == NULL ) /* If there is an error...
*/ |
| _kbhit Checks the console for keyboard input. int _kbhit( void );
Remarks The _kbhit function checks the console for a recent keystroke. If the function returns a nonzero value, a keystroke is waiting in the buffer. The program can then call _getch or _getche to get the keystroke. Subject: Console and Port I/O Routines |
||||||
| Return Value _kbhit returns a nonzero value if a key has been pressed. Otherwise, it returns 0. |
||||||
| Example /* KBHIT.C: This program loops until the user * presses a key. If _kbhit returns nonzero, a * keystroke is waiting in the buffer. The program * can call _getch or _getche to get the keystroke. */ #include <conio.h> void main( void ) /* Use _getch to throw key
away. */ |
| stdin, stdout, stderr FILE *stdin; FILE *stdout; FILE *stderr; #include <stdio.h> Remarks These are standard streams for input, output, and error output. By default, standard input is read from the keyboard, while standard output and standard error are printed to the screen. The following stream pointers are available to access the standard streams:
These pointers can be used as arguments to functions. Some functions, such as getchar and putchar, use stdin and stdout automatically. These pointers are constants, and cannot be assigned new values. The freopen function can be used to redirect the streams to disk files or to other devices. The operating system allows you to redirect a program's standard input and output at the command level. Subject: Stream I/O |
| _cgets Gets a character string from the console. char *_cgets( char *buffer );
Remarks The _cgets function reads a string of characters from the console and stores the string and its length in the location pointed to by buffer. The buffer parameter must be a pointer to a character array. The first element of the array, buffer[0], must contain the maximum length (in characters) of the string to be read. The array must contain enough elements to hold the string, a terminating null character ('\0'), and two additional bytes. The function reads characters until a carriage-return linefeed (CR-LF) combination or the specified number of characters is read. The string is stored starting at buffer[2]. If the function reads a CR-LF, it stores the null character ('\0'). _cgets then stores the actual length of the string in the second array element, buffer [1]. Because all editing keys are active when _cgets is called, pressing F3 repeats the last entry. Subject: Console and Port I/O Routines Keywords: See also _getch |
||||||
| Return Value _cgets returns a pointer to the start of the string, at buffer[2]. There is no error return. Parameter
|
||||||
| Example /* CGETS.C: This program creates a buffer and initializes * the first byte to the size of the buffer: 2. Next, the * program accepts an input string using _cgets and displays * the size and text of that string. */ #include <conio.h> void main( void ) printf( "Input line of
text, followed by carriage return:\n"); |
| _eof Tests for end-of-file. int _eof( int handle );
Remarks The _eof function determines whether the end of the file associated with handle has been reached. Subject: Error Handling Routines, Low-level I/O Routines Keywords: See also clearerr, feof, ferror, perror |
||||||
| Return Value _eof returns 1 if the current position is end of file, or 0 if it is not. A return value of 1 indicates an error; in this case, errno is set to EBADF, which indicates an invalid file handle. Parameter
|
||||||
| Example /* EOF.C: This program reads data from a file * ten bytes at a time until the end of the * file is reached or an error is encountered. */ #include <io.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> void main( void ) { int fh, count, total = 0; char buf[10]; if( (fh = _open( "eof.c", _O_RDONLY )) == - 1 ) { perror( "Open failed"); exit( 1 ); } /* Cycle until end of file reached: */ while( !_eof( fh ) ) { /* Attempt to read in 10 bytes: */ if( (count = _read( fh, buf, 10 )) == -1 ) { perror( "Read error" ); break; } /* Total actual bytes read */ total += count; } printf( "Number of bytes read = %d\n", total ); _close( fh ); } Output Number of bytes read = 754 |
| _cscanf Reads formatted data from the console. int _cscanf( const char *format [, argument] ... );
Remarks The _cscanf function reads data directly from the console into the locations given by argument. The _getche function is used to read characters. Each optional parameter must be a pointer to a variable with a type that corresponds to a type specifier in format. The format controls the interpretation of the input fields and has the same form and function as the format parameter for the scanf function. While _cscanf normally echoes the input character, it does not do so if the last call was to _ungetch. Subject: Console and Port I/O Routines Keywords: See also _cprintf, fscanf, scanf, sscanf |
||||||
| Return Value _cscanf returns the number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned. The return value is EOF for an attempt to read at end of file. This can occur when keyboard input is redirected at the operating-system command-line level. A return value of 0 means that no fields were assigned. Parameters
|
||||||
| Example /* CSCANF.C: This program prompts for a string * and uses _cscanf to read in the response. * Then _cscanf returns the number of items * matched, and the program displays that number. */ #include <stdio.h> void main( void ) _cprintf( "Enter three
integers: "); |
| gets, _getws Get a line from the stdin stream. char *gets( char *buffer ); wchar_t *_getws( wchar_t *buffer );
Remarks The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character. _getws is a wide-character version of gets; its argument and return value are wide-character strings. Generic-Text Routine Mappings
Subject: Stream I/O Routines Keywords: See also fgets, fputs, puts |
|||||||||||||||||||||
| Return Value Each of these functions returns its argument if successful. A NULL pointer indicates an error or end-of-file condition. Use ferror or feof to determine which one has occurred. Parameter
|
|||||||||||||||||||||
| Example /* GETS.C */ #include <stdio.h> void main( void ) printf( "Input a string:
" ); |
| puts, _putws Write a string to stdout. int puts( const char *string ); int _putws( const wchar_t *string );
Remarks The puts function writes string to the standard output stream stdout, replacing the string's terminating null character ('\0') with a newline character ('\n') in the output stream. Generic-Text Routine Mappings
Subject: Stream I/O Routines Keywords: See also fputs, gets |
|||||||||||||||||||||
| Return Value Each of these returns a nonnegative value if successful. If puts fails it returns EOF; if _putws fails it returns WEOF. Parameter
|
|||||||||||||||||||||
| Example /* PUTS.C: This program uses puts * to write a string to stdout. */ #include <stdio.h> void main( void ) |
| Aktuelle Daten dieser Seite | Letzte Änderung: |
| |