• Aucun résultat trouvé

fclose - fcloseall

Dans le document Language Reference (Page 186-200)

The fclose and fcloseall functions close a stream or streams. These functions flush all buffers associated with the streams before closing them. When they close the stream, these functions release the buffers that the system reserved. They do not automatically release buffers that the setbuf routine assigned.

The fclose function closes the given stream. The fcloseall function closes all open streams except the stdin, stdout, stderr, stdaux, and stdprn streams, and any temporary files created by tmpfile.

The fclose function returns 0 if it successfully closes the stream. The fcloseall function returns the total number of streams closed. Both functions return EOF to indicate an error.

fclose - fcloseall

Example:

The following example opens a file

data

for reading as a data stream;

then, it closes this file. It closes all other streams except the stdin, stdout, stderr, stdaux, and stdprn data streams.

#include <stdio.h>

FILE *stream;

int numclosed;

stream = fopen("data", "r");

/* The following statement closes the stream. */

fclose(stream);

/* The following statement closes all *

* streams except stdin, stdout, *

* stderr, stdaux, and stdprn. */

numclosed = fcloseall();

Related Topics:

close, fdopen, fflush, fopen, freopen

Purpose:

Converts a floating-point number to a character string.

Format:

/* Required for function declarations */

#include <stdlib.h>

char *fcvt(value, ndee, decptr, signptr) /* Number to be converted */

double value;

/* Number of digits after decimal point */

int ndee;

/* Pointer to stored decimal point position */

int *deeptr;

/* Pointer to stored sign indicator */

int *signptr;

Comments:

fcvl

The fcvt function converts a floating-point number to a character string. The value is the floating-point number to be converted. The fcvl function stores the digits of value as a string and adds a null character (\0). The ndee variable specifies the number of digits to be stored after the decimal point.

If the number of digits after the decimal point in value exceeds ndee, fcvl rounds the correct digit according to thE:) FORTRAN F format. If therE:) are fewer than ndee digits of precision, fcvt pads the string with zeros.

The fcvlfunction stores only digits in the string. You can obtain the position of the decimal point and the sign of value after the call from deeptr and signptr. The deeptr variable points to an integer value giving the position of the decimal point with respect to the beginning of the string. A zero or negative integer value shows that the decimal point lies to the left of the first digit. The signptr variable points to an integer showing the sign of value. The fcvl function sets the integer

fevt

The fcvl function returns a pointer to the string of digits. There is no error return value. Because of the limited precision of the double type, no more than 16 decimal digits are significant in any conver-sion.

Example:

This example will read in two floating-paint numbers, compute their product, and print out only the billions digit of its character

representation. At most, 16 decimal digits of significance can be expected.

printf("Enter two floating-point"

"numbers.\n");

if(21",(i"'scanf("%e %e",&x.&y))) {

printf("input error ... \n");

abort () ;

printf("Their product does not"

" exceed one bi 11 ion. \n") ; if(b>15)

pri ntf ("The bi 11 ions di git of II

"their product is insignificant.\n");

if«b>-1)&&(b<16))

printf("The billions digit of"

" their product is %c.\n", buffer[b]);

fcvl

Related Topics:

atof, atoi, atol, ecyl, gcyl

Note: The ecyl and fcyl functions use a single, statically-allocated buffer for the conversion. Each call to one of these functions destroys the result of the previous call.

fdopen

Purpose:

Associates an input or output stream with the file identified by a handle.

Format:

#include <stdio.h>

FILE *fdopen(handle, type)

int handle; /* Handle referring to open file */

char *type; /* Type of access permitted * / Comments:

The fdopen function associates an input or output stream with the file identified by handle. The type variable is a character string speci-fying the type of access requested for the file.

Type

Open a text file for reading. (The file must exist.)

Create a text file for writing. If the given file exists, erase its contents.

Open a text file for writing additional data at the end of the file. Create the file first if it does not exist.

Open a text file for both reading and writing. (The file must exist.)

Create a text file for both reading and writing. If the given file exists, erase its contents.

Open a text file for reading and for writing additional data to the end of the file. Create the file first if it does not exist.

"rb" Open a binary file for reading. The file must exist.

"wb" Create a binary file for writing. If the given file exists, its contents are destroyed.

fdopen

"ab" Open a binary file for writing at the end of that file. Create the file first if it does not exist.

"r+b" or "rb+"

Open a binary file for both reading and writing. The file must exist.

"w+b" or "wb+"

Create a binary file for both reading and writing. If the given file exists, fdopen erases its contents.

"a+b" or "ab+"

Open a binary file for both reading and adding to its end.

Create the file first if it does not exist.

CAUTION:

Use the "w", "w+", "wb", "wb+", and "w+b" modes with care. They can destroy flies.

The specified type must be compatible with the access mode you used to open the file.

When you open a file with "a" or "a+" as the value of type, all write operations take place at the end of the file. Although you can reposi-tion the file pointer, using fseek or rewind, the file pointer always moves back to the end of the file before the system carries out any write operation. This action prevents you from writing over existing data.

When you specify any of the types containing "+" you can both read from and write to the file. The file is said to be open for "update."

However, when switching from reading to writing, or the reverse, you must include an intervening fseek or rewind operation. You can specify the current position for the fseek operation.

In accessing text files carriage return-linefeed (CR-LF) combinations are translated into a single linefeed (LF) on input; linefeed characters are translated to CR-LF combinations on output. Accesses to binary files suppress all of these translations.

The fdopen function returns a pointer to the open stream. A NULL pointer value shows an error.

'dopen

Example:

The following example opens the file DATA and associates its file handle fh with the pointer stream.

#include <stdio.h>

#include <fcntl.h>

FILE *stream;

int fh;

fh = open("data", O_RDONLY);

/* The following statement associates a *

* stream with the open file handle. */

stream = fdopen(fh, "r");

Related Topics:

dup, dup2, tclose, tcloseall, topen, treopen, open

feof

Purpose:

Tests the end-of-file indicator for a stream.

Format:

#include <stdio.h>

int feof(stream)

FILE *stream; /* Pointer to a file structure * / Comments:

The feof function tells whether you have reached the end of the given stream. After you reach the end-of-file character, read operations return an end-of-file indicator until you close the stream or call clearerr, fseek, or rewind.

The feof function returns a nonzero value after the first read operation which attempts to read past the end-of-file character. The feof func-tion returns the value 0 if no attempt has been made to read past the end-of-file character. There is no error-return value.

Example:

The following example scans the input file STREAM until it reads an end-of-file character.

#include <stdio.h>

char string[lOO];

FILE *stream;

void process(char *);

main() {

while (!feof(stream))

if (fscanf(stream, "%s", string)) process(string);}

feof

Related Topics:

clearerr, eof, ferror, perror

Note: The feot routine is a macro. Since an empty file has no end-of-file character, calls to the teof for an empty stream always returns O.

ferror

Purpose:

Tests for errors in reading from or writing to a stream.

Format:

#include <stdio.h>

irit ferror(stream)

FILE *stream; /* Pointer to file structure */

Comments:

The terror function tests for an error in reading from or writing to the given stream. If an error occurs, the error indicator for the stream remains set until the you close stream, rewind, or call clearerr.

The terror function returns

a

nonzero value to indicate an error on the given stream. A return value of 0 means no error has occurred.

Example:

The following example puts data out to a stream, and then checks to make sure a write error has not occurred. You must have previously opened the stream for writing.

#include <stdio.h>

FILE *stream;

char *string;

fprintf(stream, "%s\n", string};

if (ferror(stream}) { printf("write error"};

clearerr(stream};

}

terror

Related Topics:

clearerr, eof, feof, fopen, perror Note: The ferror routine is a macro.

fflush

Purpose:

Causes the system to write the contents of a buffer to a file.

Format:

#include <stdio.h>

int fflush(stream)

FILE *stream; /* Pointer to a file structure *1 Comments:

The fflush function causes the system to write the contents of the buffer associated with the specified output stream to a file. If the stream is open for input, fflush clears the contents of the buffer. The fflush function negates the effect of any prior call to ungetch for the stream. The stream remains open after the call. The fflush function has no effect on an unbuffered stream.

The fflush function returns the value 0 if it successfully flushes the buffer. It also returns the value 0 in cases where the specified stream has no buffer or is open for reading only. A return value of EOF shows an error.

Example:

The following statements flush a stream buffer and set up a new buffer for that stream.

#include <stdio.h>

FILE *stream;

char buffer[BUFSIZ];

fflush(stream);

setbuf(stream,buffer);

"lush

Related Topics:

fclose, flushall, setbuf

Note: The system automatically flushes buffers when they are full, when you close the stream, or when a program ends normally without closing the stream.

Purpose:

Frees a storage block.

Format:

/* Required qnly for function declarations */

#include <malloc.h>

void _ffree(ptr)

/* Pointer to reserved storage block */

void far *ptr;

Comments:

ffree

The _ffree function frees a storage block outside the default data segment. Ptr points to a storage block previously reserved through a call to _'malloe. The number of bytes freed is the number specified when the block was allocated. After the cal.I, the freed block is again available. If ptr is NULL, _ffree ignores it.

Note: Attempting to free an incorrect ptr (a pointer not reserved with _'malloe) can affect subsequent attempts to reserve storage and cause errors. In the small and medium models, a call to 'ree is equivalent to a call to _n'ree. In the compact and large models, a call to'ree is equivalent to a call to _ffree.

Example:

The following example illustrates the reserving and freeing of 100 bytes.

#include <malloc.h>

#include <stdio.h>

void far *alloc;

alloc = _fmalloc(100);

if (alloc != NULL) /* Test for a valid pointer */

_ffree(alloc); /* Free storage for the heap */

Dans le document Language Reference (Page 186-200)