• Aucun résultat trouvé

Stream Routines

Dans le document Language Reference (Page 55-59)

The stream routines treat a data file or data item as a stream of indi-vidual characters. By choosing among the many stream functions available, you can process data in different sizes and formats, from single characters to large data structures~

When you open a file for input or output using the stream functions, the system associates the opened file with a structure of type FILE,

defined in stdio.h, containing basic information about the file. The system returns a pointer to the FILE structure when you open the stream. This pointer, called the stream pointer or just stream, refers to the file in subsequent operations.

The stream functions can provide for optionally buffered and for-matted input and output. When you direct a stream to a buffer, the system collects data rea~ from or written to the stream in an interme-diat~ storage location called a buffer. During a write operation, the system writes the contents of the output buffer t9 the appropriate final location only after the buffer is full, When the stream is closed,or when the program ends normally. To carry out this operation is to flush th~ buffer .. During a read operation, the system places a block of data in the input buffer and reads the data from the buffer. The system transfers the next block of data into the buffer only after the buffer is empty.

Buffering lets input or output proceed efficiently because the system can transfer a large block of data in a single operation rather than performing an input or output operation each time it reads a data item from or writes one to a stream. However, if a program ends abnor-mally, the system might not flush the output buffers and could lose data.

Clears the error indicator for a

stream.

Closes a

stream.

Reads a character from a

stream

(function version).

Reads a character from stdin (function version).

Reads a string from

stream.

Gets the file handle associated with a

stream.

Flushes all

streams.

Opens a

stream.

Writes formatted data to a

stream.

Writes a character to a

stream

(function version).

Writes a character to stdout (function version).

Writes a stri ng to a

stream.

Reads unformatted data items from a

stream.

Reassigns a FILE pointer.

Reads formatted data from a

stream.

RepOSitions the file pointer to a given location.

Gets the current file pointer position.

Writes fixed-length data items to stdout.

Reads a character from a

stream

(macro version).

Reads a character from stdin (macro version).

Reads a line from stdin.

Reads a bi nary int from a

stream.

Writes formatted data to stdout.

Writes a character to a

stream

(macro version).

Writes a character to stdout (macro version).

Writes a line to a

stream.

Writes a bi nary int to a

stream.

Repositions file pointer to beginning of a

stream.

Removes temporary files created by a tmpfile.

scanf

Reads formatted data from stdin.

Controls stream bufferi ng.

Controls stream buffering and buffer size.

Writes formatted data to a string.

Reads formatted data from a stri ng.

Produces a temporary file name in a given directory.

Creates a temporary file.

Produces a temporary file name.

Places a character in the buffer.

Writes formatted data to a stream.

Writes formatted data to stdout.

Writes formatted data to a string.

To use the stream functions, you must include the stdio.h file in your program. This file defines constants, types, and structures used in the stream functions and contains function declarations and macro definitions for the stream routines.

Some constants defined in stdio.h can be useful in your program.

The manifest constant EOF is the value returned at the end of a file.

NULL is the null pointer. FILE is the structure that maintains informa-tion about a stream. BUFSIZ defines the size of stream buffers in bytes.

Opening a Stream: You must open a stream with the fdopen, fopen, or freopen function before you can perform input or output on that stream. You can open a stream for reading, writing, or both. You can open a stream in text mode or bi nary mode.

The fdopen, fopen, and freopen functions return a FILE pointer, which refers to the stream. When you call one of these functions, assign the return value to a FILE pointer variable and use that variable to refer to the opened stream. For example, if your program contains the line:

infile = fopen ("test.dat", "r");

you can use the FILE pointer variable infile to refer to the stream.

Predefined Stream Pointers: stdin, stdout, stderr, stdaux, stdprn:

When a program begins to run, the system automatically opens five streams. These streams are the standard input, standard output, standard error, standard auxiliary, and standard print streams. By default, the standard input, standard output, and standard error streams refer to the keyboard and screen. Whenever a program expects input from the standard input stream, it receives that input from the keyboard. Similarly, a program that writes to the standard output stream displays data on the screen. The system sends error messages produced by the library routines to the standard error stream. The error messages interrupt the standard output stream and appear on the screen.

The assignment of the standard a'uxiliary stream and the standard print stream depends on the machine setup. These streams usually refer to an auxiliary port and a printer, respectively, but they might not have a device attached on a particular system. Be sure to check your machine setup before using these streams.

When you use the stream functions, you can refer to the standard input, standard output, standard error, standard auxiliary, and standard print streams by using the following predefined FILE pointers.

You can use these pointers in any function that requires a stream pointer as an argument. Some functions, such as getchar and putchar, use stdin or stdout automatically. The pointers stdin, stdout, stderr, stdaux, and stdprn are constants, not variables. Do not try to assign them a new stream pointer value. Pointers stdaux and stdprn are not predefined under OS/2.

You can use the DOS redirection symbols

«,>,

or » ) or the pipe symbol (I) to redefine the standard input and standard output for a particular program. See the IBM Disk Operating System Technical Reference book for a complete discussion of redirection and pipes.

For example, if you run a program and redirect its output to a file named RESULTS, the program writes to the RESULTS file each time the standard output is specified in a write operation. You do not change the program when you redirect the output. You change only the file associated with stdout for a single run of the program.

You can redefine stdin, stdout, stderr, stdaux, or stdprn to refer to a disk file or to a device. The freopen routine reassigns the stream.

See the freopen function in Chapter 5, "Library Routines," for a description of this option.

Note: You cannot redirect stderr (the standard error stream) at the

DOS command level, though you can from OS/2. For example, use the command line

c1 main.c 2>errmsg

in OS/2 to redirect the error stream from the compiler to a file called ERRMSG.

Dans le document Language Reference (Page 55-59)