fopen

Source: http://www.cplusplus.com/reference/cstdio/fopen/


用法:  FILE * fopen ( const char * filename, const char * mode );

Summary:

Opens the specified name in the file name in the parameter file and is associated with a stream, which may be identified by the file pointer returned in later operations.
Stream allows operations and how to perform the operations defined by the mode parameter.
If you know the return flow does not refer to an interactive device, the default will be fully buffered (See setbuf) case.
The pointer returned by calling fclose can disassociate or freopen file. All open files are automatically closed when the normal program termination.
Operating environment to support at least open simultaneously fopen_max file.


 

parameter:

filename

To open C string containing the file name.
Its value is the file name should follow the specifications operating environment, and may include a path (if the system supports).

mode

r
w
a
r+
w+
a+
Read: open the file for input operation. The file must exist.
Write: Create an empty file is output operations. If a file with the same name already exists, its contents are discarded, and the file as a new empty file.
Additional: open the file, the output end of the file. Output operation data is always written at the end of the file, and expand it. Repositioning operations (fseek, fsetpos, rewind) will be ignored. If the file does not exist, then create the file.
Read / Update: open a file is updated (input and output). The file must exist.
Write / update: create an empty file and open it for update (input and output). If the file already exists, its contents will be discarded and treated as a new empty files in the file.
Adding / Update: open a file is updated (including input and output), the output operation of writing all data in the end of the file. Repositioning operations (fseek, fsetpos, rewind) under the influence of an input operation, but the operation will output position back end of the file. If the file does not exist, then create the file.

 

 

 

 

 

 

 

 

Using the above mode specifier, the file as a text file is opened. In order to open the file as a binary file, the pattern string must contain the "B" character. This additional "b" may be appended to the end character string (to form a compound of the following modes: "rb", "wb" , "ab", "r + b", "w + b", "a + b" ) may be inserted letters and mixed-mode "+" sign ( "+ rb", "+ wb", between "ab +").
The new C standard (not part of C ++) adds a new standard "x" ( "x") , can be attached to any "W" specifier (formation of "0", "0", "w" or "W "+" + "). If the file exists, this parameter will force the sub-function fails, instead of rewriting it.
If there are other characters behind the sequence, then the behavior depends on the library realize: Some implementations may ignore other characters in order to receive extra "t" (sometimes used to explicitly declare a text file).
In some implementations, the library, use the update mode to open or create a text file might flow treated as binary files.

Text file is a text file that contains a sequence of lines. The environmental applications running in text mode input / output operations may be some special character conversion, to adapt them to the system-specific text file format. Although the conversion does not exist in some circumstances, and the text and binary files are processed in the same manner, but using the appropriate model to improve portability.

While allowing for the input and output operations of the open file update (including the "+" sign in the file), before performing a read operation after the write operation, should be refreshed (fflush) or repositioning stream (fseek, fsetpos, rewind) . Before the write operation (operation when the end of file is not reached), the stream should reposition (fseek, fsetpos, rewind) after a read operation.


return value:

If the file has been successfully opened, then the function returns a pointer to the file object, the object can be used in the subsequent identification operation flow.
Otherwise, it returns a null pointer.
In most library implementations, Erro variable is also set to system-specific error code.

 

Guess you like

Origin www.cnblogs.com/eternalmoonbeam/p/11613134.html