PS-Trainer C - Entwicklung
Bibliotheks-Funktionen: StreamIO
Homepage von PS-Trainer - C-Entwicklung - Bibliotheken - an PS-Trainer
PS-Trainer PS-Trainer

StreamIO Data Stream Input & Output
Standard streams for input, output, and error output.
stdin, stdout, stderr  
_eof Tests for end-of-file.
   
Standard Formatted input and output
printf, wprintf Print formatted output to the standard output stream.
scanf, wscanf Read formatted data from the standard input stream.
vprintf, vwprintf Write formatted data to stdout
Format control string for printf and scanf functions
Standard Character input and output
_fgetchar, _fgetwchar Read character from stdin (function versions of getchar and getwchar)
getchar, getwchar Get a character from stdin .
putchar, putwchar Writes a character to stdout.
_fputchar, _fputwchar Write character to stdout (function versions of putchar and putwchar)
Standard Line input and output
gets, _getws Get a line from the stdin stream.
puts, _putws Write a string to stdout.
Error messages
perror, _wperror Print an error message.
strerror, _strerror Get a system error message (strerror) or prints a user-supplied error message (_strerror).
Console
_kbhit Checks the console for keyboard input.
_getch, _getche Get a character from the console without echo (_getch) or with echo (_getche).
_putch Writes a character to the console.
_ungetch Pushes back the last charcter read from the console.
_cgets Gets a character string from the console.
_cscanf Reads formatted data from the console.
File Handling
_creat, _wcreat Creates a new file.
rename, _wrename Rename file
remove, _wremove Delete file
_unlink, _wunlink Delete file
File open & close
_open, _wopen Open a file
_sopen, _wsopen Open a file for sharing.
_dup, _dup2 Create a second handle for an open file (_dup), or reassign a file handle (_dup2).
freopen, _wfreopen Reassign a file pointer.
_chmod, _wchmod Change the file-permission settings.
_fileno Gets the file handle associated with a stream.
_close Close a file
File read & write
   
ftell Get current file position
rewind Move file position to beginning of stream
fseek Move file position to given location
ferror Tests for an error on a stream.
_setmode Sets the file translation mode (Text, Binary)
fflush Flushes a stream.
_flushall Flushes all streams; clears all buffers.
Stream open & close
   
   
   
_fcloseall Close all open streams except stdin, stdout, and stderr
Stream read & write
_getw Read binary int from stream
_putw Write binary int to stream
fgetc, fgetwc Read character from stream (function versions of getc and getwc)
getc, getwc Read character from stream (macro versions of fgetc and fgetwc)
putc, putwc Writes a character to a stream
fputc, fputwc Write a character to a stream (function versions of putc and putwc)
ungetc, ungetwc Push character back onto stream
fgets, fgetws Read string from stream
fread Read unformatted data from stream
fwrite Write unformatted data items to stream
puts, _putws Write line to stream
fputs, fputws Write string to stream
vfprintf, vfwprintf Write formatted data to stream
fscanf, fwscanf Read formatted data from stream
fprintf, fwprintf Write formatted data to stream
fgetpos Get position indicator of stream
fsetpos Set position indicator of stream
feof Tests for end-of-file on a stream.
ferror Tests for an error on a stream.
clearerr Resets the error indicator for a stream
   

 


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.
clearerr Clear error indicator for stream
fclose Close stream
_fcloseall Close all open streams except stdin, stdout, and stderr
_fdopen, wfdopen Associate stream with handle to open file
feof Test for end of file on stream
ferror Test for error on stream
fflush Flush stream to buffer or storage device
fgetc, fgetwc Read character from stream (function versions of getc and getwc)
_fgetchar, _fgetwchar Read character from stdin (function versions of getchar and getwchar)
fgetpos Get position indicator of stream
fgets, fgetws Read string from stream
_fileno Get file handle associated with stream
_flushall Flush all streams to buffer or storage device
fopen, _wfopen Open stream
fprintf, fwprintf Write formatted data to stream
fputc, fputwc Write a character to a stream (function versions of putc and putwc)
_fputchar, _fputwchar Write character to stdout (function versions of putchar and putwchar)
fputs, fputws Write string to stream
fread Read unformatted data from stream
freopen, _wfreopen Reassign FILE stream pointer to new file or device
fscanf, fwscanf Read formatted data from stream
fseek Move file position to given location
fsetpos Set position indicator of stream
_fsopen, _wfsopen Open stream with file sharing
ftell Get current file position
fwrite Write unformatted data items to stream
getc, getwc Read character from stream (macro versions of fgetc and fgetwc)
getchar, getwchar Read character from stdin (macro versions of fgetchar and fgetwchar)
gets, getws Read line from stdin
_getw Read binary int from stream
printf, wprintf Write formatted data to stdout
putc, putwc Write character to a stream (macro versions of fputc and fputwc)
putchar, putwchar Write character to stdout (macro versions of fputchar and fputwchar)
puts, _putws Write line to stream
_putw Write binary int to stream
rewind Move file position to beginning of stream
_rmtmp Remove temporary files created by tmpfile
scanf, wscanf Read formatted data from stdin
setbuf Control stream buffering
_setmaxstdio Set a maximum for the number of simultaneously open files at the stream I/O level.
setvbuf Control stream buffering and buffer size
_snprintf, _snwprintf Write formatted data of specified length to string
sprintf, swprintf Write formatted data to string
sscanf, swscanf Read formatted data from string
_tempnam, _wtempnam Generate temporary filename in given directory
tmpfile Create temporary file
tmpnam, _wtmpnam Generate temporary filename
ungetc, ungetwc Push character back onto stream
vfprintf, vfwprintf Write formatted data to stream
vprintf, vwprintf Write formatted data to stdout
_vsnprintf, _vsnwprintf Write formatted data of specified length to buffer
vsprintf, vswprintf Write formatted data to buffer
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 );

Routine Required Header Compatibility
_getw <stdio.h> Win 95, Win NT

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
stream Pointer to FILE structure
Example
/* GETW.C: This program uses _getw to read a word
* from a stream, then performs an error check.
*/

#include <stdio.h>
#include <stdlib.h>

void main( void )
{
FILE *stream;
int i;

if( (stream = fopen( "getw.c", "rb" )) == NULL )
printf( "Couldn't open file\n" );
else
{
/* Read a word from the stream: */
i = _getw( stream );

/* If there is an error... */
if( ferror( stream ) )
{
printf( "_getw failed\n" );
clearerr( stream );
}
else
printf( "First data word in file: 0x%.4x\n", i );
fclose( stream );
}
}


Output
First data word in file: 0x47202a2f


_kbhit
Checks the console for keyboard input.
int _kbhit( void );

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

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>
#include <stdio.h>

void main( void )
{
/* Display message until key is pressed. */
while( !_kbhit() )
_cputs( "Hit me!! " );

/* Use _getch to throw key away. */
printf( "\nKey struck was '%c'\n", _getch() );
_getch();
}


Output
Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!!
Key struck was 'q'


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:

Pointer Stream
stdin Standard input
stdout Standard output
stderr Standard error

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 );

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

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
buffer Storage location for data
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>
#include <stdio.h>

void main( void )
{
char buffer[82] = { 80 }; /* Maximum characters in 1st byte */
char *result;

printf( "Input line of text, followed by carriage return:\n");
result = _cgets( buffer ); /* Input a line of text */
printf( "\nLine length = %d\nText = %s\n", buffer[1], result );
}


Output
Input line of text, followed by carriage return:
This is a line of text
Line length = 22
Text = This is a line of text.


_eof
Tests for end-of-file.
int _eof( int handle );

Function Required Header Compatibility
_eof <io.h> <errno.h> Win 95, Win NT

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
handle Handle referring to open file
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] ... );

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

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
format Format-control string
argument Optional 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>
#include <conio.h>

void main( void )
{
int result, i[3];

_cprintf( "Enter three integers: ");
result = _cscanf( "%i %i %i", &i[0], &i[1], &i[2] );
_cprintf( "\r\nYou entered " );
while( result-- )
_cprintf( "%i ", i[result] );
_cprintf( "\r\n" );
}


Output
Enter three integers: 1 2 3
You entered 3 2 1


gets, _getws
Get a line from the stdin stream.
char *gets( char *buffer );
wchar_t *_getws( wchar_t *buffer );


Routine Required Header Compatibility
gets <stdio.h> ANSI, Win 95, Win NT
_getws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT

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
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_getts gets gets _getws

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
buffer Storage location for input string
Example
/* GETS.C */

#include <stdio.h>

void main( void )
{
char line[81];

printf( "Input a string: " );
gets( line );
printf( "The line entered was: %s\n", line );
}

Output
Input a string: Hello!
The line entered was: Hello!


puts, _putws
Write a string to stdout.
int puts( const char *string );
int _putws( const wchar_t *string );


Routine Required Header Compatibility
puts <stdio.h> ANSI, Win 95, Win NT
_putws <stdio.h> ANSI, Win 95, Win NT

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
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_putts puts puts _putws

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
string Output string
Example
/* PUTS.C: This program uses puts
* to write a string to stdout.
*/

#include <stdio.h>

void main( void )
{
puts( "Hello world from puts!" );
}


Output
Hello world from puts!


Homepage von PS-Trainer - C-Entwicklung - Bibliotheken - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1