• Aucun résultat trouvé

Character Input/Output Functions

Dans le document III CP·6 (Page 193-198)

The character input/output functions are described in the following subsections.

fgetc Function Synopsis:

#include <stdio.h>

int fgetc(FILE *stream);

Description:

The fgetc function obtains the next character (if present) as an unsigned char converted to an int, from the input stream pointed to by stream, and advances the associated file position indicator for the stream.

Returns:

The fgetc function returns the next character from the input stream pointed to by stream.

If the stream is at end-of-file, the end-of-file indicator for the stream is set and fgetc returns EOF. If a read error occurs, the error indicator for the stream is set and fgetc ret urns EOF. 10

fgets Function Synopsis:

#include <stdio.h>

char *fgets(char *s, int n, FILE *stream);

Description:

The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

Returns:

The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.

10 An end-of-file and a read error can be distinguished by use of the feof and ferror functions.

HA17-00 fgets Function 16-25

Input/Output <stdio. h> Functions fput c Function

Synopsis:

#include <stdio.h>

int fputc(int c, FILE *stream);

Description:

The fputc function writes the character specified by c (converted to an unsigned char) to the output stream pointed to by stream, at the position indicated by the associated file position indicator for the stream, and advances the indicator appropriately. If the file cannot support positioning requests, or if the stream was opened with append mode, the character is appended to the output stream.

Returns:

The fputc function returns the character written. If a write error occursj the error indicator for the stream is set and fput c ret urns EOF.

fput s Function Synopsis:

#include <stdio.h>

int fputs(const char *s, FILE *stream);

Description:

The fputs function writes the string pointed to by s to the stream pointed to by stream.

The terminating null character is not written.

Returns:

The fputs function returns EOF if a write error occurs; otherwise it returns a non-negative value.

16-26 fputs Function HA17-00

Input/Output <stdio. h> Functions gete Function

Synopsis:

#inelude <stdio.h>

int gete(FILE *stream);

Description:

The gete function is equivalent to fgete, except that since it is implemented as a macro, it evaluates stream more than once, so that argument should never be an expression with side effects.

Returns:

The gete function returns the next character from the input stream pointed to by stream.

If the stream is at end-of-file, the end-of-file indicator for the stream is set and gete returns EOF. If a read error occurs, the error indicator for the stream is set and gete returns EOF.

get char Function . Synopsis:

#inelude <stdio.h>

int getehar(void);

Description:

The get char function is equivalent to gete with the argument stdin.

Returns:

The getehar function returns the next character from the input stream pointed to by stdin. If the stream is at end-of-file, the end-of-file indicator for the stream is set and getehar returns EOF. If a read error occurs, the error indicator for the stream is set and get char ret urns EOF.

HA17-00 getehar Function 16-27

Input/Output <stdio. h> Functions get s Function

Synopsis:

#include <stdio.h>

char *gets(char *s);

Description:

The gets function reads characters from the input stream pointed to by stdin, into the array pointed to by s, until end-of-file is encountered or a new-line character is read. Any new-line character is discarded, and a null character is written immediately after the last character read into the array.

Returns:

The gets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.

putc Function Synopsis:

#include <stdio.h>

int putc(int c, FILE *stream)j Description:

The putc function is equivalent to fputc, except that since it is implemented as a macro, it evaluates stream more than once, so that argument should never be an expression with side effects.

Returns:

The putc function returns the character written. If a write error occurs, the error indicator for the stream is set and putc returns EOF.

16-28 putc Function HA17-00

Input/Output <stdio. h> Functions putchar Function

Synopsis:

#include <stdio.h>

int putchar(int c);

Description:

The putchar function is equivalent to putc with the second argument stdout.

Returns:

The putchar function returns the character written. If a write error occurs, the error indicator for the stream is set and putchar returns EOF.

puts Function Synopsis:

#include <stdio.h>

int puts(const char *s);

Description:

The puts function writes the string pointed to by s to the stream pointed to by stdout, and appends a new-line character to the output. The terminating null character is not written.

Returns:

The puts function returns EOF if a write error occurs; otherwise it returns a non-negative value.

ungetc Function Synopsis:

#include <stdio.h>

int ungetc(int c, FILE *stream)j

HA17-00 unget c Function 16-29

Input/Output <stdio. h> Functions Description:

The ungetc function pushes the character specified by c (converted to an unsigned char) back onto the input stream pointed to by stream. The pushed-back character will be returned by a subsequent read on that stream. A successful intervening call to a file positioning function (fseek, fsetpos, or rewind) with the same stream discards the pushed- back character for the stream. The external storage corresponding to the stream is unchanged.

One character of pushback is available. If the ungetc function is called more than once on the same stream without an intervening read or file positioning operation on that stream, the operation fails.

If the value of c equals that of the macro EOF, the operation fails and the input stream is unchanged.

A successful call to the ungetc function clears the end-of-file indicator for the stream. The value of the file position indicator for the stream after reading or discarding the pushed-back character is the same as it was before the character was pushed pushed-back. For a text stream, the value of its file position indicator after a successful call to the ungetc function is unspecified until the pushed-back character is read or discarded. For a binary stream, its file position indicator is decremented; if its value was zero before a call, it is indeterminate after the call.

Returns:

The ungetc function returns the character pushed back after converSlon, or EOF if the operation fails.

Dans le document III CP·6 (Page 193-198)