• Aucun résultat trouvé

File Access Functions

Dans le document III CP·6 (Page 177-181)

The file access functions are described in the following subsections.

fclose Function Synopsis:

#include <stdio.h>

int fclose(FILE *stream);

Description:

The fclose function causes the stream pointed to by stream to be flushed and the associated file to be closed. Any unwritten buffered data for the stream are written to the file; any unread buffered data are discarded. The stream is disassociated from the file.

If the associated buffer was automatically allocated, it is deallocated.

Returns:

The f close function returns zero if the stream was successfully closed, or EOF if any errors were detected.

fflush Function Synopsis:

#include <stdio.h>

int fflush(FILE *stream);

Description:

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be written to the file.

If stream is a null pointer, the fflush function performs this flushing action on all streams.

Returns:

The fflush function returns EOF if a write error occurs; otherwise it returns zero.

HA17-00 fflush Function 16-9

Input / Output < stdio . h> Functions

The argument mode points to a string beginning with one of the following sequences:

r intervening calls to the fseek function.

When a file is opened with update mode (, +, as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output may not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek , fsetpos, or rewind), and input may not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. Opening (or creating) a text file with update mode opens (or creates) a binary stream.

When opened, a stream is fully buffered if and only if it can be determined not to refer to an interactive device. The error and end-of-file indicators for the stream are cleared.

The CP-6 version of the fopen function permits a specific DeB to be opened by supplying an illegal file name in one of two forms which has special interpretation:

16-10 fopen Function HA17-00

Input / Output <stdio. h> Functions

When a DeB is explicitly requested, the mode argument to fopen is treated differently when the DeB has been "set" with one of the following options: device or file type dependent). The initial mode may optionally be followed by a blank and keywords separated by blanks request various options. The "fopen mode keywords" table contains the keywords accepted by fopen. If a keyword is found which is not recognized,

If creating, catalog file immediately.

Allow multiple readers or one updater.

Allow one updater and multiple readers.

Allow multiple updaters and readers.

Table 16-1. fopen M ode Keywords

fopen Function 16-11

Input / Output <stdio. h> Functions

I

Keyword

I ~:a:!;! opens,

,...""+ density to 800 bpi.

d800 i:n.:'L

d1600 For tape opens, set density to 1600 bpi.

d6250 For tape opens, set density to 6250 bpi.

terminal Program will supply all terminal positioning.

x364 Translate X3.64 controls for terminal type.

ur If creating, create a unit-record file.

keyed If creating, create a keyed file.

consec If creating, create a consecutive file.

comp If creating, compress records in disk file.

Table 16-1. fopen Mode Keywords (part 2) Returns:

The fopen function returns a pointer to the

FILE

object controlling the stream. If the open operation fails, fopen returns a null pointer.

freopen Function Synopsis:

#include <stdio.h>

FILE

*freopen(const char *filename, const char *mode,

FILE

*stream);

Description:

The freopen function opens the file whose name is the string pointed to by filename and associates the stream pointed to by stream with it. The mode argument is used just as in the fopen function.3

The freopen function first attempts to close any file that is associated with the specified stream. Errors encountered while closing the stream are not reported. The error and end-of-file indicators for the stream are cleared.

Returns:

The freopen function returns a null pointer if the open operation fails. Otherwise, freopen returns the value of stream.

3 The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout), as those identifiers are not modifiable lvalues to which the value returned by the f open function may be assigned.

16-12 freopen Function HA17-00

Input / Output <stdio. h> Functions setbuf Function

Synopsis:

#include <stdio.h>

void setbuf(FILE *stream, char *buf);

Description:

Except that it returns no value, the setbuf function is equivalent to the setvbuf function invoked with the values _IOFBF for mode and BUFSIZ for size, or, if buf is a null pointer, with the value _IONBF for mode.

Returns:

The setbuf function returns no value.

setvbuf Function Synopsis:

#include <stdio.h>

int setvbuf(FILE *stream, char *buf, int mode, size t size);

Description:

The setvbuf function may be used only after the stream pointed to by stream has been associated with an open file and before any other operation is performed on the stream.

The argument mode determines how stream will be buffered, as follows: _IOFBF causes input/output to be fully buffered; _IOLBF causes input/output to be line buffered; _IONBF causes input/output to be unbuffered. If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by the setvbuf function.4The argument size specifies the size of the array. The contents of the array at any time are indeterminate.

Returns:

The setvbuf function returns zero on success, or nonzero if an invalid value is given for mode or if the request cannot be honored.

Dans le document III CP·6 (Page 177-181)