c fopen檔案讀寫
fopen
FILE * fopen ( const char * filename, const char * mode );
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.
With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
Additional characters may follow the sequence, although they should have no effect. For example, "t" is sometimes appended to make explicit the file is a text file.
In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability.
For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.
Return Value
If the file has been successfully opened the function will return a pointer to a FILE object that is used to identify the stream on all further operations involving it. Otherwise, a null pointer is returned.
Example
|
|
fopen ( string filename, string mode )
返回值是 FILE*
fopen() 中的 mode 的可能值列表
mode | 說明 |
---|---|
'r' | 只讀方式開啟,將檔案指標指向檔案頭。 |
'r+' | 讀寫方式開啟,將檔案指標指向檔案頭。 |
'w' | 寫入方式開啟,將檔案指標指向檔案頭並將檔案大小截為零。如果檔案不存在則嘗試建立之。 |
'w+' | 讀寫方式開啟,將檔案指標指向檔案頭並將檔案大小截為零。如果檔案不存在則嘗試建立之。 |
'a' | 寫入方式開啟,將檔案指標指向檔案末尾。如果檔案不存在則嘗試建立之。 |
'a+' | 讀寫方式開啟,將檔案指標指向檔案末尾。如果檔案不存在則嘗試建立之。 |
'x' | 建立並以寫入方式開啟,將檔案指標指向檔案頭。如果檔案已存在,則 fopen() 呼叫失敗並返回 FALSE,並生成一條 E_WARNING 級別的錯誤資訊。如果檔案不存在則嘗試建立之。這和給 底層的 open(2) 系統呼叫指定 O_EXCL|O_CREAT 標記是等價的。此選項被 PHP 4.3.2 以及以後的版本所支援,僅能用於本地檔案。 |
'x+' | 建立並以讀寫方式開啟,將檔案指標指向檔案頭。如果檔案已存在,則 fopen() 呼叫失敗並返回 FALSE,並生成一條 E_WARNING 級別的錯誤資訊。如果檔案不存在則嘗試建立之。這和給 底層的 open(2) 系統呼叫指定 O_EXCL|O_CREAT 標記是等價的。此選項被 PHP 4.3.2 以及以後的版本所支援,僅能用於本地檔案。 |
在操作二進位制檔案時如果沒有指定 'b' 標記,可能會碰到一些奇怪的問題,包括壞掉的圖片檔案以及關於 \r\n 字元的奇怪問題。
為移植性考慮,強烈建議在用 fopen() 開啟檔案時總是使用 'b' 標記。
本文轉自莫水千流部落格園部落格,原文連結:http://www.cnblogs.com/zhoug2020/p/5851733.html,如需轉載請自行聯絡原作者
相關文章
- C/C++ 檔案讀寫C++
- C++讀寫檔案C++
- C++檔案讀寫C++
- C語言-檔案讀寫C語言
- C++讀寫檔案操作C++
- c風格讀寫檔案
- C++檔案讀寫操作C++
- 使用C#讀寫ini檔案C#
- 使用C#讀寫xml檔案C#XML
- C#讀寫檔案總結C#
- C#讀取文字檔案和寫文字檔案C#
- C#關於讀寫INI檔案C#
- C++學習筆記----讀寫檔案C++筆記
- 【C++基礎】檔案流讀寫操作C++
- 檔案排版(文字檔案讀寫)
- 檔案的讀寫
- Python——檔案讀寫Python
- keras讀寫檔案Keras
- 「Python」:檔案讀寫Python
- Golang 讀、寫檔案Golang
- Python 讀寫檔案Python
- 普通檔案的讀寫
- python檔案讀寫操作Python
- python讀寫excel檔案PythonExcel
- VBA建立文字檔案、讀寫文字檔案
- C++檔案操作實戰:建立、寫入、讀取、修改檔案一應俱全C++
- C++寫檔案操作C++
- Python:讀寫檔案(I/O) | 組織檔案Python
- 讀取檔案流並寫入檔案流
- Perl讀寫檔案&字串操作字串
- java 讀寫 ini 配置檔案Java
- nodejs xmlreader 讀寫xml檔案NodeJSXML
- Python中的檔案讀寫Python
- Python 檔案讀寫(Python IO)Python
- Golang對檔案讀寫操作Golang
- Java 字元流檔案讀寫Java字元
- C語言讀取寫入CSV檔案 [一]基礎篇C語言
- 重拾C#日常積累:config配置檔案的讀寫C#
- C/C++讀取SEGY檔案(三)C++