Watcom C Library Reference : ferror, fflush, fgetc, fgetchar, fgetpos, fgets
Watcom C Reference/D - E - F 2021. 6. 25. 12:49
Watcom C Library Reference : ferror, fflush, fgetc, fgetchar, fgetpos, fgets
ferror
Synopsis : #include <stdio.h>
int ferror( FILE *fp ):
Description : The ferror function tests the error indicator for the stream pointed to by fp.
Returns : The ferror function returns non-zero if the error indicator is set for fp.
See Also : clearerr, feof, perror, strerror
Example :
#include <stdio.h>
void main( )
{
FILE *fp;
int c;
fp = fopen( "file", "r" );
if( fp != NULL ) {
c = fgetc( fp );
if( ferror( fp ) ) {
printf( "Error reading file\n" );
}
}
fclose( fp );
}
Classification : ANSI
Systems : All
fflush
Synopsis : #include <stdio.h>
int fflush( FILE *fp );
Description : If the file fp is open for output or update, the fflush function causes any unwritten data to be written to the file. If the file fp is open for input or update, the fflush function undoes the effect of any preceding ungetc operation on the stream. If the value of fp is NULL, then all files that are open will be flushed.
Returns : The fflush function returns non-zero if a write error occurs and zero otherwise. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fgetc, fgets, flushall, fopen, getc, gets, setbuf, setvbuf, ungete
Example :
#include <stdio.h>
#include <conio.h>
void main( )
{
printf( "Press any key to continue..." );
fflush( stdout );
getch( );
}
Classification : ANSI
Systems : All
fgetc
Synopsis : #include <stdio.h>
int fgetc ( FILE *fp );
Description : The fgetc function gets the next character from the file designated by fp. The character is signed.
Returns : The fgetc function returns the next character from the input stream pointed to by fp. If the stream is at end-of-file, the end-of-file indicator is set and fgetc returns EOF. If a read error occurs, the error indicator is set and fgetc returns EOF. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fgetchar, fgets, fopen, getc, gets, ungetc
Example :
#include <stdio.h>
void main( )
{
FILE *fp;
int c;
fp = fopen( "file", "r" );
if( fp != NULL) {
while( (c = fgetc ( fp) ) != EOF ) {
fputc( c, stdout );
}
fclose( fp );
}
}
Classification : ANSI
Systems : All
fgetchar
Synopsis : #include <stdio.h>
int fgetchar( void );
Description : The fgetchar function is equivalent to fgetc with the argument stdin.
Returns : The fgetchar function returns the next character from the input stream pointed to by stdin. If the stream is at end-of-file, the end-of-file indicator is set and fgetchar returns EOF. If a read error occurs, the error indicator is set and fgetchar returns EOF. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fgetc, getc, getchar
Example :
#include <stdio.h>
void main( )
{
FILE *fp;
int c;
fp = freopen( "file", "r", stdin );
if( fp != NULL ) {
while( (c = fgetchar()) != EOF ) {
fputchar( c );
}
fclose( fp );
}
}
Classification : WATCOM
Systems : All
fgetpos
Synopsis : #include <stdio.h>
int fgetpos ( FILE *fp, fpos_t *pos );
Description : The fgetpos function stores the current position of the file fp in the object pointed to by pos. The value stored is usable by the fsetpos function for repositioning the file to its position at the time of the call to the fgetpos function.
Returns : The fgetpos function returns zero if successful, otherwise, the fgetpos function returns a non-zero value. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fopen, fseek, fsetpos, ftell
Example :
#include <stdio.h>
void main( )
{
FILE *fp;
fpos_t position;
auto char buffer[80];
fp = fopen( "file", "r" );
if( fp != NULL ) {
fgetpos ( fp, &position ); /* get position */
fgets ( buffer, 80, fp); /* read record */
fsetpos ( fp, &position ); /* set position */
fgets ( buffer, 80, fp); /* read same record */
fclose( fp );
}
}
Classification : ANSI
Systems : All
fgets
Synopsis : #include <stdio.h>
char *fgets( char *buf, size_t n, FILE * fp );
Description : The fgets function gets a string of characters from the file designated by fp and stores them in the array pointed to by buf. The fgets function stops reading characters when end-of-file is reached, or when a newline character is read, or when n-1 characters have been read, whichever comes first. The new-line character is not discarded. A null character is placed immediately after the last character read into the array.
A common programming error is to assume the presence of a new-line character in every string that is read into the array. A new-line character will not be present when more than n-1 characters occur before the new-line. Also, a new-line character may not appear as the last character in a file, just before end-of-file.
The gets function is similar to fgets except that it operates with stdin, it has no size argument, and it replaces a newline character with the null character.
Returns : The fgets function returns buf if successful. NULL is returned if end-of-file is encountered, or a read error occurs. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fopen, getc, gets, fgetc
Example :
#include <stdio.h>
void main( )
{
FILE *fp;
char buffer[80];
fp = fopen( "file", "r" );
if( fp != NULL ) {
while( fgets ( buffer, 80, fp) != NULL ) {
fputs ( buffer, stdout );
}
fclose( fp );
}
}
Classification : ANSI
Systems : All
This manual describes the WATCOM C library for DOS, Windows, and OS/2, It includes the Standard C Library (as defined in the ANSI C Standard).
WATCOM C Language Reference manual describes the ANSI C Programming language and extensions to it which are supported by WATCOM C/C++ (32bit)