Part IV • Documenting the Differences
fgets()
Header: |
stdio.h |
Syntax: |
char * fgets(char * szBuffer, int |
|
BufferSize, FILE * OpenFile); |
Description: |
Gets a string from the file, stopping when either a newline |
|
character is encountered or |
|
BufferSize - 1 characters have been read. |
Parameters: |
szBuffer—Buffer to store characters in. |
|
BufferSize—Size of the buffer. |
|
OpenFile—Pointer to a FILE structure for an opened input file. |
Returns: |
NULL if an error occurs; otherwise, szBuffer is returned. |
Example: |
char szBuffer[100]; |
|
gets(szBuffer, sizeof(szBuffer), |
|
OpenFile); |
Note: |
A newline character is never discarded. Don’t assume there will |
|
always be a newline character; test to be sure. |
floor()
Header: |
math.h |
Syntax: |
double floor(double dValue); |
Description: |
Returns the largest integer (converted to double) that is not greater |
|
than dValue. |
Parameters: |
dValue—Value to use for the computation. |
Returns: |
Double value representing the largest integer not larger than |
|
dValue. |
Example: |
double dFloor = floor(3.14159); |
|
/* dFloor will be 3.0 */ |
Note: |
See ceil(). |
Part IV • Documenting the Differences
Table 14.1. File opening mode letter descriptions.
Mode |
|
Character |
Description |
|
|
r |
Read (cannot be used with write, w, or append, a). |
w |
Write (cannot be used with read, r, or append, a). |
a |
Append (cannot be used with read, r, or write, w). |
b |
Binary (cannot be used with text, t). |
t |
Text (cannot be used with binary, b). |
+ |
Opens for both read and write (used with read and write). |
|
With write, truncates file to zero length. |
|
|
fprintf()
Header: stdio.h
Syntax: int fprintf(FILE * OpenFile, const char * szFormat, ...);
Description: Does formatted output to the file pointed to by OpenFile.
Parameters: OpenFile—Pointer to an open stream (text mode) file. szFormat—A format descriptor string.
Returns: Number of characters written. If negative, then an error occurred.
Example: fprintf(stderr, “The number one is” “%d\n”, 1);
Note: See the section on printf() format codes at the end of this chapter.
fputc()
Header: stdio.h
Syntax: int fputc(int nCharacter, FILE * OpenFile);
Part IV • Documenting the Differences
Parameters: Array—Pointer to the array (which may be a character array).
ElementSize—Size of each element in the array.
NumberElements—Number of elements in array.
OpenFile—Pointer to an opened file.
Returns: Number of elements read.
Example: int nTimes[20];
fread(nTimes, sizeof(nTimes[0]), sizeof(nTimes) / sizeof(nTimes[0]), OpenFile);
Note: The number of elements read may be less than the number requested.
free()
Header: |
malloc.h & stdlib.h |
Syntax: |
void free(void * Pointer); |
Description: |
Frees thememory (which was allocated with calloc()or malloc()) |
|
pointed to by Pointer. |
Parameters: |
Pointer—Pointer to a dynamically allocated memory block. |
Returns: |
No return value. |
Example: |
int *nArray = calloc(20, sizeof(int)); |
|
free(nArray); |
Note: |
See calloc() and malloc(). |
freopen()
Header: stdio.h
Syntax: FILE * freopen(const char * szFileName, const char * szMode, FILE *
OpenFile);
Part IV • Documenting the Differences
fscanf()
Header: stdio.h
Syntax: int fscanf(FILE * OpenFile, const char * szFormat, ...);
Description: Reads formatted input from the specified stream (text mode) file, with the format of the input determined by the format string pointed to by szFormat.
Parameters: OpenFile—Pointer to an opened file.
szFormat—Format string (see the section on format strings below).
Returns: Number of arguments scanned or EOF if the end of the stream was reached.
Example: fscanf(OpenFile, “%s %d”, szBuffer,
&nCount);
Note: fscanf() has a variable number of arguments determined by the format string.
fseek()
Header: stdio.h
Syntax: int fseek(FILE * OpenFile, long lOffset, int nOrigin);
Description: Moves the file pointer for the specified file to the position specified by lOffset relative to the origin specified by nOrigin.
Parameters: OpenFile—Pointer to an opened file.
lOffset—Where to move the file pointer.
nOrigin—Origin point from which to compute the new file pointer position.
Returns: Zero if the function is successful, non-zero if it fails.
ANSI C’s Library Functions |
C C C |
|
14C |
|
C C C |
|
C C |
Example: fseek(OpenFile, 256l, SEEK_CUR);
/* Skip the next 256 bytes */
Note: Table 14.2 lists the valid seek origins.
Table 14.2. File seek origins.
From the start of the file (a negative value for the offset value is not acceptable).
From the current position of the file’s pointer (either a negative or positive value for the offset value is acceptable).
From the end of the file (a negative value for the offset value is acceptable).
You cannot seek before the beginning of a file, but you can seek past the end of the file.
fsetpos()
Header: stdio.h
Syntax: int fsetpos(FILE * OpenFile, const fpos_t *
Position);
Description: Sets a file’s position, using the fpos_t variable filled in using fgetpos().
Parameters: OpenFile—Pointer to an opened file.
Position—Pointer to an fpos_t data object, filled in using fgetpos().
Returns: Zero if successful, otherwise a non-zero value.
Part IV • Documenting the Differences
Example: |
fpos_t Position; |
|
Position = 100; |
|
/* Position is now at byte 100. */ |
|
fsetpos(OpenFile, &Position); |
Note: |
Also see fgetpos(). |
ftell()
Header: |
stdio.h |
|
Syntax: |
long ftell(FILE * OpenFile); |
Description: |
Returns the current read or write point for the specified file. |
Parameters: |
OpenFile—Pointer to an opened file. |
Returns: |
The read or write position for the file. |
Example: |
long |
lPosition = ftell(OpenFile); |
Note: |
The result received from ftell() can later be used with fseek(). |
fwrite()
Header: |
stdio.h |
Syntax: |
size_t fwrite(const void * Array, size_t |
|
ElementSize, size_t |
|
NumberElements, FILE * |
|
OpenFile); |
Description: |
Writes NumberElements of Array to the specified file. |
Parameters: |
Array—Pointer to an array (often a character string). |
|
ElementSize—Size of each element in the array. |
|
NumberElements—Number of elements to write. |
|
OpenFile—Pointer to an opened file. |
Returns: |
Number of elements written. If the returned value is less than |
|
NumberElements then an error occurred. |