Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
93
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

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().

536

 

 

ANSI C’s Library Functions

 

 

 

fmod()

 

 

Header:

math.h

 

Syntax:

double fmod(double x, double y);

Description:

Returns the remainder of x / y.

Parameters:

x—Numerator, double value to be divided.

 

y—Denominator, double value to divide by.

Returns:

Remainder of the division.

Example:

double

dMod = fmod(3.14159, 3.0);

 

/* dMod will be 0.14159 */

Note:

If y is non-zero, then the result has the same sign as x.

fopen()

C C C

C14C C

C C C

Header:

stdio.h

Syntax:

FILE * fopen(const char * szFileName, const

 

char * Mode);

Description:

Opens the file, using the filename and mode provided.

Parameters:

szFileName—Pointer to a character string containing a valid

 

filename.

 

Mode—Pointer to a character string containing the mode descrip-

 

tor characters.

Returns:

Pointer to a FILE structure for the file that was opened or NULL if

 

the file couldn’t be opened.

Example:

FILE * OurFile;

 

OurFile = fopen(“ourfile.dat”, “r”);

Note:

The mode characters include those shown in Table 14.1, which

 

follows. Each character can be used with other characters except

 

where indicated otherwise.

537

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

538

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

Description:

 

C C

Writes the character contained in nCharacter to the file pointed

 

to by OpenFile.

 

Parameters:

nCharacter—Character to be written.

 

 

OpenFile—Pointer to an opened file.

 

Returns:

The character written or EOF if an error occurs.

 

Example:

fputc(‘!’, stderr);

 

 

fputc(‘\n’, stderr);

 

Note:

Use errno to determine what error occurred.

 

fputs()

 

 

Header:

stdio.h

 

Syntax:

int fputs(const char * szBuffer, FILE *

 

 

OpenFile);

 

Description:

Writes the string pointed to by szBuffer to the file specified.

Parameters:

szBuffer—Pointer to a string to be written.

 

 

OpenFile—Pointer to an opened file.

 

Returns:

EOF if an error occurs, otherwise a non-negative value.

 

Example:

fputs(“Now is the time...\n”, stderr);

 

Note:

Also see fprintf().

 

fread()

 

 

Header:

stdio.h

 

Syntax:

size_t fread(void * Array, size_t

 

 

ElementSize, size_t

 

 

NumberElements, FILE *

 

 

OpenFile);

 

Description:

Reads an array from the file.

 

539

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

540

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

Description:

 

 

C C

Allows a specific file to be associated with an already opened file.

 

Usually used to allow stdin, or one of the other pre-opened

 

standard files, to be associated with a specific file.

 

Parameters:

szFileName—Pointer to the filename of the file to be opened.

 

szMode—Mode string (see fopen() for details).

 

 

OpenFile—Pointer to an opened file.

 

Returns:

Pointer to a FILE structure.

 

Example:

FILE * File = freopen(“OurFile.dat”, “r”,

 

 

 

stdin);

 

 

/* scanf() will now read from ‘OurFile.dat’

 

 

*/

 

 

Note:

Also see fopen().

 

frexp()

 

 

 

Header:

math.h

 

 

Syntax:

double frexp(double dValue, int *

 

 

 

nExponent);

 

Description:

Normalizes a floating point number and places the exponent in

 

the integer pointed to by nExponent.

 

Parameters:

dValue—Floating point value to be normalized.

 

 

nExponent—Pointer to an integer to hold the exponent (2 raised

 

to the .power nExponent).

 

Returns:

The parameter dValue normalized.

 

Example:

int

nExponent;

 

 

double

dNormal = frexp(3.14159,

 

 

 

&nExponent);

 

 

/* dNormal will be 0.785398, nExponent will

 

 

be 2 */

 

Note:

In the preceding example 0.785398 * (2 * 2) = 3.14159.

 

541

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.

542

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.

Origin point

Description

SEEK_SET

From the start of the file (a negative value for the offset value is not acceptable).

SEEK_CUR

From the current position of the file’s pointer (either a negative or positive value for the offset value is acceptable).

SEEK_END

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.

543

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.

544

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

Example:

int

nArray[] = {1,2,3,4,5,6,7,8,9};

C C

 

 

 

fwrite(nArray,

 

 

 

sizeof(nArray[0]),

 

 

 

sizeof(nArray) / sizeof(nArray[0]),

 

 

 

OpenFile);

 

Note:

Check errno if an error occurred, to determine what the error is.

getc()

 

 

 

Header:

stdio.h

 

Syntax:

int getc(FILE * OpenFile);

 

Description:

Gets the next character from the specified file.

 

Parameters:

OpenFile—Pointer to an opened file.

 

Returns:

The character retrieved from the file or EOF if there is an error.

Example:

char

chChar;

 

 

 

chChar = (char)getc(stdin);

 

 

/* Gets one character from the keyboard */

 

Note:

This function is generally equal to fgetc() except that it may be

 

implemented as a macro.

 

getchar()

 

 

 

Header:

stdio.h

 

Syntax:

int getchar(void);

 

Description:

Gets the next character from stdin.

 

Parameters:

None.

 

Returns:

The next character from stdin.

 

Example:

char

chChar;

 

 

 

chChar = getchar();

 

Note:

This function is the same as using getc(stdin).

 

545