关于fopen的,文件使用方式中我看得懂三个r/w/a,分别是、、。
但是其他几个就莫名其妙了,比如"r+"、"w+"、"rb+"、"wb+"——谭浩强书上的解释全部都是!我晕~~
它们几个到底区别何在啊?
比如"r"/"r+"/"rb"/"rb+"这四个到底区别在哪里啊?
"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, or fseek operation. The current position can be specified for the fsetpos or fseek operation, if desired.
In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters:
t
Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file.
Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters. For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters.
b
Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.
If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL.
For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.
c
Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.
n
Reset the commit flag for the associated filename to “no-commit.” This is the default.
書上寫明了
"rb+"、"wb+"
是對二進制文件的操作!
b是二进制的缩写,这些都是针对二进制操作的!
我粗略说一下。还是要自己学好英文
"r+"
Opens for both reading and writing. (The file must exist.)
以可读可写方式打开文件,也就是这种方式允许读和写。
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
以读写方式打开文件,如果文件存在,他的内容被摧毁,也就是删除。如果没有文件,建立文件
"a+"
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
以读和追加的方式打开文件。追加操作包括在有新的数据写入文件之前移除EOF标记。在写操作完成后,再加上EOF标记。如果文件不存在,则建立新文件。
When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
当文件是以"a"或"a+"方式打开的,所有的写操作发生在文件的末尾。文件指针可以使用fseek改变,但是总是在任何写操作执行时移动到文件的末尾。因此,已存在的数据不会被修改。
我上面说的以读写方式打开就是这种方式允许读和写。
还有默认的打开方式是以文本方式打开的,也就是加了"t",不过这个t可以省略。
如果加上"b",是以二进制方式打开的。至于问什么文件要以二进制方式读写,这个你应该知道。
下面的两个标记我从来没用过。就不解释了