Watcom C Library Reference : fscanf, fseek, fsetpos, _fsopen
fscanf
Synopsis : #include <stdio.h>
int fscanf ( FILE *fp, const char *format, ... );
Description : The fscanf function scans input from the file designated by fp under control of the argument format. Following the format string is a list of addresses to receive values. The format string is described under the description of the scanf function.
Returns : The fscanf function returns EOF when the scanning is terminated by reaching the end of the input stream. Otherwise, the number of input arguments for which values were successfully scanned and stored is returned. When a file input error occurs, the errno global variable may be set.
See Also : cscanf, scanf, sscanf, vcscanf, vfscanf, vscanf, vsscanf
Example : To scan a date in the form "Friday April 13 1999":
#include <stdio.h>
void main( )
{
int day, year;
char weekday[10], month[10];
FILE *in_data;
in_data = fopen( "file", "r" );
if( in_data != NULL ) {
fscanf ( in_data, "%s %s %d %d", weekday, month, &day, &year );
printf( "Weekday=%s Month=%s Day=%d Year=%d\n", weekday, month, day, year );
fclose( in_data );
}
}
Classification : ANSI
Systems : All
fseek
Synopsis : #include <stdio.h>
int fseek ( FILE *fp, long int offset, int where );
Description : The fseek function changes the read/write position of the file specified by fp. This position defines the character that will be read or written on the next I/O operation on the file. The argument fp is a file pointer returned by fopen or freopen. The argument offset is the position to seek to relative to one of three positions specified by the argument where. Allowable values for where are:
Value | Meaning |
SEEK_SET | The new file position is computed relative to the start of the file. The value of offset must not be negative. |
SEEK_CUR | The new file position is computed relative to the current file position. The value of offset may be positive, negative or zero. |
SEEK_END | The new file position is computed relative to the end of the file. |
The fseek function clears the end-of-file indicator and undoes any effects of the ungetc function on the same file.
The ftell function can be used to obtain the current position in the file before changing it The position can be restored by using the value returned by ftell in a subsequent call to fseek with the where parameter set to SEEK_SET.
Returns : The fseek function returns zero if successful, non-zero otherwise. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fgetpos, fopen, fsetpos, ftell
Example : The size of a file can be determined by the following example which saves and restores the current position of the file.
#include <stdio.h>
long int filesize( FILE *fp )
{
long int save_pos, size_of_file;
save_pos = ftell( fp );
fseek ( fp, 0L, SEEK_END );
size_of_file = ftell( fp );
fseek( fp, save_pos, SEEK_SET );
return( size_of_file );
}
void main( )
{
FILE *fp;
fp = fopen( "file", "r" );
if ( fp != NULL ) {
printf( "File size=%d\n", filesize( fp) );
fclose( fp );
}
}
Classification : ANSI
Systems : All
fsetpos
Synopsis : #include <stdio.h>
int fsetpos ( FILE *fp, fpos_t *pos );
Description : The fsetpos function positions the file fp according to the value of the object pointed to by pos, which shall be a value returned by an earlier call to the fgetpos function on the same file.
Returns : The fsetpos function returns zero if successful, otherwise, the fsetpos 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 : fgetpos, fopen, fseek, 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
_fsopen
Synopsis : #include <stdio.h>
FILE *_fsopen( const char *filename, const char *mode, int share );
Description : The _fsopen function opens the file whose name is the string pointed to by filename, and associates a stream with it. The arguments mode and share control shared reading or writing The argument mode points to a string beginning with one of the following sequences:
Mode | Meaning |
"r" | open file for reading; use default file translation |
"w" | create file for writing, or truncate to zero length; use default file translation |
"a" | append: open text file or create for writing at end-of-file; use default file translation |
"rb" | open binary file for reading |
"rt" | open text file for reading |
"wb" | create binary file for writing, or truncate to zero length |
"wt" | create text file for writing, or truncate to zero length |
"ab" | append; open binary file or create for writing at end-of-file |
"at" | append; open text file or create for writing at end-of-file |
"r+" | open file for update (reading and/or writing); use default file translation |
"w+" | create file for update, or truncate to zero length; use default file translation |
"a+" | append; open file or create for update, writing at end-of-file; use default file translation |
"r+b", "rb+" | open binary file for update (reading and/or writing) |
"r+t", "rt+" | open text file for update (reading and/or writing) |
"w+b", "wb+" | create binary file for update, or truncate to zero length |
"w+t", "wt+" | create text file for update, or truncate to zero length |
"a+b", "ab+" | append; open binary file or create for update, writing at end-of-file |
"a+t", "at+" | append; open text file or create for update, writing at end-of-file |
When default file translation is specified, the value of the global variable _fmode establishes whether the file is to treated as a binary or a text file. Unless this value is changed by the program, the default will be text mode.
Opening a file with read mode ('r' as the first character in the mode argument) fails if the file does not exist or it cannot be read. Opening a file with append mode ('a' as the first character in the mode argument) causes all subsequent writes to the file to be forced to the current end-of-file, regardless of previous calls to the fseek function. When a file is opened with update mode ('+' as the second or third character of the mode argument), both input and output may be performed on the associated stream.
When a stream is opened in update mode, both reading and writing may be performed. However, writing may not be followed by reading without an intervening call to the fflush function or to a file positioning function ( fseek, fsetpos, rewind). Similarly, reading may not be followed by writing without an intervening call to a file positioning function, unless the read resulted in end-of-file.
The shared access for the file, share, is established by a combination of bits defined in the <share.h> header file. The following values may be set:
Value | Meaning |
SH_COMPAT | Set compatibility mode. |
SH_DENYRW | Prevent read or write access to the file. |
SH_DENYWR | Prevent write access of the file. |
SH_DENYRD | Prevent read access to the file. |
SH_DENYNO | Permit both read and write access to the file. |
You should consult the technical documentation for the DOS system that you are using for more detailed information about these sharing modes.
Returns : The _fsopen function returns a pointer to the object controlling the stream. This pointer must be passed as a parameter to subsequent functions for performing operations on the file. If the open operation fails, _fsopen returns NULL. When an error has occurred, errno contains a value indicating the type of error that has been detected.
See Also : fclose, fcloseall, fdopen, fopen, freopen
Example :
#include <stdio.h>
void main( )
{
FILE *fp;
/* open a file and prevent others from writing to it */
fp = _fsopen( "report.dat", "W", SH_DENYWR );
if( fp != NULL ) {
/* rest of code goes here */
fclose( fp );
}
}
Classification : WATCOM
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)