fgets, fputs, ferror, feof, stdin, stdout, EOF, fopen, fclose, fgetc, fputc, getchar, putchar
注意区分
gets, fgets, fgetc, getc, getchar
puts, fputs, fputc, putc, putchar
fgets
function<cstdio>
char * fgets ( char * str, int num, FILE * stream );
Get string from stream
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
A null character is automatically appended in str after the characters read to signal the end of the C string.
fputs
function<cstdio>
int fputs ( const char * str, FILE * stream );
Write string to stream
Writes the string pointed by str to the stream.
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to the stream.
fgetc
function<cstdio>
int fgetc ( FILE * stream );Get character from stream
Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced by one character to point to the next character.
fgetc and getc are equivalent, except that the latter one may be implemented as a macro.Parameters
- stream
- Pointer to a FILE object that identifies the stream on which the operation is to be performed.
Return Value
The character read is returned as an int value.
If the End-of-File is reached or a reading error happens, the function returns EOF and the corresponding error or eof indicator is set. You can use either ferror or feof to determine whether an error happened or the End-Of-File was reached.fputc
function<cstdio>
int fputc ( int character, FILE * stream );Write character to stream
Writes a character to the stream and advances the position indicator.
The character is written at the current position of the stream as indicated by the internal position indicator, which is then advanced one character.Parameters
- character
- Character to be written. The character is passed as its int promotion.
- stream
- Pointer to a FILE object that identifies the stream where the character is to be written.
Return Value
If there are no errors, the same character that has been written is returned.
If an error occurs, EOF is returned and the error indicator is set (see ferror).getc
function<cstdio>
int getc ( FILE * stream );Get character from stream
Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced by one character to point to the next character.
getc is equivalent to fgetc and also expects a stream as parameter, but getc may be implemented as a macro, so the argument passed to it should not be an expression with potential side effects.
See getchar for a similar function without stream parameter.Parameters
- stream
- pointer to a FILE object that identifies the stream on which the operation is to be performed.
Return Value
The character read is returned as an int value.
If the End-of-File is reached or a reading error happens, the function returns EOF and the corresponding error or eof indicator is set. You can use either ferror or feof to determine whether an error happened or the End-Of-File was reached.putc
function<cstdio>
int putc ( int character, FILE * stream );Write character to stream
Writes a character to the stream and advances the position indicator.
The character is written at the current position of the stream as indicated by the internal position indicator, which is then advanced one character.
putc is equivalent to fputc and also expects a stream as parameter, but putc may be implemented as a macro, so the argument passed should not be an expression with potential side effects.
See putchar for a similar function without stream parameter.getchar
function<cstdio>
int getchar ( void );Get character from stdin
Returns the next character from the standard input (stdin).
It is equivalent to getc with stdin as its argument.putchar
function<cstdio>
int putchar ( int character );Write character to stdout
Writes character to the current position in the standard output (stdout) and advances the internal file position indicator to the next position.
It is equivalent to putc(character,stdout).
ferror
function<cstdio>
int ferror ( FILE * stream );Check error indicator
Checks if the error indicator associated with stream is set, returning a value different from zero if it is.
This indicator is generaly set by a previous operation on the stream that failed.
feof
function<cstdio>
int feof ( FILE * stream );Check End-of-File indicator
Checks whether the End-of-File indicator associated with stream is set, returning a value different from zero if it is.
This indicator is generally set by a previous operation on the stream that reached the End-of-File.
Further operations on the stream once the End-of-File has been reached will fail until either rewind, fseek or fsetpos is successfully called to set the position indicator to a new value.
stdin
object<cstdio>
FILE * stdin;Standard input stream
The standard input stream is the default source of data for applications. It is usually directed to the input device of the standard console (generally, a keyboard).
stdin can be used as an argument for any function that expects an input stream as one of its parameters, like fgets or fscanf.
stdout
object<cstdio>
FILE * stdout;Standard output stream
The standard output stream is the default destination of regular output for applications. It is usually directed to the output device of the standard console (generally, the screen).
stdout can be used as an argument for any function that expects an output stream as one of its parameters, like fputs or fprintf.EOF
constant<cstdio>
End-of-File
It is a macro constant definition. It expands to a negative integral constant expression.
It is used as the value returned by several <cstdio> functions to indicate failure, either because the End-of-File has been reached in a reading operation or because an error happened.fopen
function<cstdio>
FILE * fopen ( const char * filename, const char * mode );Open file
Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE object whose pointer is returned. The operations that are allowed on the stream and how these are performed are defined by the mode parameter.
The running environment supports at least FOPEN_MAX files open simultaneously; FOPEN_MAX is a macro constant defined in <cstdio>.Parameters
- filename
- C string containing the name of the file to be opened. This paramenter must follow the file name specifications of the running environment and can include a path if the system supports it.
- mode
- C string containing a file access modes. It can be:
"r" Open a file for reading. The file must exist. "w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+" Open a file for update both reading and writing. The file must exist. "w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist. fclose
function<cstdio>
int fclose ( FILE * stream );Close file
Closes the file associated with the stream and disassociates it.
All internal buffers associated with the stream are flushed: the content of any unwritten buffer is written and the content of any unread buffer is discarded.
Even if the call fails, the stream passed as parameter will no longer be associated with the file.Parameters
- stream
- Pointer to a FILE object that specifies the stream to be closed.
Return Value
If the stream is successfully closed, a zero value is returned.
On failure, EOF is returned.