fopen 中文man頁面
NAME
fopen, fdopen, freopen - 打開流
SYNOPSIS (總覽)
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fildes, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);
DESCRIPTION 描述
函數 fopen 打開 一個 文件, 并且 分配 一個 流, 文件名 由 字符串 path 指定.
參數 mode 指向 一個 字符串, 該 字符串 用 下面 的 字符串 開頭 (之后 可以有 附加的 字符):
- r
- 為 讀操作 打開 文本文件. 流 被定位于 文件 的 開始.
- r+
- 為 讀寫操作 打開 文本文件. 流 被定位于 文件 的 開始.
- w
- 為 寫操作 創建 文本文件, 或者 將 已經 存在的 文件長度 截斷為 零. 流 被定位于 文件 的 開始.
- w+
- 為 讀寫操作 打開 文件. 如果 文件 不存在, 就 創建 它, 否則 將它 截斷. 流 被定位于 文件 的 開始.
- a
- 為 追加操作 (在文件尾寫) 打開 文件. 如果 文件 不存在, 就 創建 它. 流 被定位于 文件 的 末尾.
- a+
- 為 追加操作 (在文件尾寫) 打開 文件. 如果 文件 不存在, 就 創建 它. 讀文件的初始位置 是 文件 的 開始, 但是 寫文件 總是 被追加到 文件 的 末尾.
可以 把 字母 ``b'' 添加到 字符串 mode 的 末尾, 或者 插到 上面 任何 兩個字符的 字符串 的 中間. 這樣 只是 為了 和 ANSI X3.159-1989 (``ANSI C'') 標準 嚴格 保持 兼容, 沒有 實際的 效果; 在 所有遵循 POSIX 的 系統 中, ``b'' 都 被忽略, 包括 Linux. (其他系統 可能會 分別對待 文本文件 和 二進制文件, 如果 在 進行 二進制文件 的 I/O, 那么 添加 ``b'' 是個 好主意, 因為 你的程序 可能會 被移植到 非 Unix 環境中.)
任何 新建文件 的 訪問模式 是 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH (0666), 并且 用 進程的 掩碼值 umask 加以修改 (參見 umask(2)).
在 讀/寫流 上 可以 任意 順序 混合 進行 讀寫操作. 注意 ANSI C 要求 在 輸出和輸入操作 之間 插入 一個 文件定位函數, 除非 輸入操作遇到了 文件結束符. (如果 不是 這種情況, 那么 讀操作 總是 返回 寫操作 的 結果而不是 最后的 內容.) 因此, 最好 (在 Linux 中 有時 是必須的) 對 這樣的流 的 寫/讀操作 之間 加入 一個 fseek 或是 fgetpos 操作. 這個操作 可以 是一個 空操作 (如 fseek(..., 0L, SEEK_CUR)), 只利用其 文件同步 這個 副效應.
用 追加方式 打開 文件 (a 作為 mode 的 第一個 字符) 將使得 所有后續的 寫操作 發生在 文件末尾, 如同 之前 調用了
- fseek(stream,0,SEEK_END);
一樣.
fdopen 函數 將 一個流 和 已存在的 文件描述符 fildes 聯系 起來. 流的 操作模式 mode (取值為 "r", "r+", "w", "w+", "a", "a+" 之一) 必須 與 該文件描述符 的 操作模式一致. 流的 定位標識 設置為 fildes 原有的值, 清除 錯誤標記 和 文件結束標記. 模式 "w" 或者 "w+" 不會 截斷 文件. fdopen 不復制 文件描述符, 在關閉 fdopen 創建的 流 時, 也不關閉 該文件描述符. 對 共享內存對象 實施 fdopen 的 結果 沒有定義.
freopen 函數 打開 用 path 說明 的 文件, 并且 和 stream 指定的流 聯系 起來. 原來的流 (如果 存在的話) 被關閉. 參數 mode 和 fopen 中的 用法 一樣. freopen 函數 主要的用處 是 改變 標準文本流 (stderr, stdin, 或 stdout) 聯系 的 文件.
RETURN VALUE (返回值)
如果 操作 成功, fopen, fdopen 和 freopen 返回 一個 指向 文件對象 FILE 的 指針, 否則 返回 NULL 并 設置 全局變量 errno 來 指出 錯誤的發生.
ERRORS
- EINVAL
- fopen, fdopen, 或 freopen 提供的 參數 mode 無效.
fopen, fdopen 和 freopen 也有可能 失敗 并置 errno 為 malloc(3) 指定的值.
fopen 也有可能 失敗 并置 errno 為 open(2) 指定的值.
fdopen 也有可能 失敗 并置 errno 為 fcntl(2) 指定的值.
freopen 也有可能 失敗 并置 errno 為 open(2), fclose(3) 和 fflush(3) 指定的值.
CONFORMING TO (標準參考)
fopen 和 freopen 函數 遵循 ANSI X3.159-1989 (``ANSI C'') 標準. fdopen 遵循 IEEE Std1003.1-1988 (``POSIX.1'') 標準.
SEE ALSO (另見)
open(2), fclose(3), fileno(3)
#p#
NAME
fopen, fdopen, freopen - stream open functions
SYNOPSIS
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fildes, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);
DESCRIPTION
The fopen function opens the file whose name is the string pointed to by path and associates a stream with it.
The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):
- r
- Open text file for reading. The stream is positioned at the beginning of the file.
- r+
- Open for reading and writing. The stream is positioned at the beginning of the file.
- w
- Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
- w+
- Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
- a
- Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
- a+
- Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
The mode string can also include the letter ``b'' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with ANSI X3.159-1989 (``ANSI C'') and has no effect; the ``b'' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the ``b'' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-Unix environments.)
Any created files will have mode S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH (0666), as modified by the process' umask value (see umask(2)).
Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek or fgetpos operation between write and read operations on such a stream. This operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.
Opening a file in append mode (a as the first character of mode) causes all subsequent write operations to this stream to occur at end-of-file, as if preceded by an
- fseek(stream,0,SEEK_END);
call.
The fdopen function associates a stream with the existing file descriptor, fildes. The mode of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") must be compatible with the mode of the file descriptor. The file position indicator of the new stream is set to that belonging to fildes, and the error and end-of-file indicators are cleared. Modes "w" or "w+" do not cause truncation of the file. The file descriptor is not dup'ed, and will be closed when the stream created by fdopen is closed. The result of applying fdopen to a shared memory object is undefined.
The freopen function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen function. The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout).
RETURN VALUE
Upon successful completion fopen, fdopen and freopen return a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error.
ERRORS
- EINVAL
- The mode provided to fopen, fdopen, or freopen was invalid.
The fopen, fdopen and freopen functions may also fail and set errno for any of the errors specified for the routine malloc(3).
The fopen function may also fail and set errno for any of the errors specified for the routine open(2).
The fdopen function may also fail and set errno for any of the errors specified for the routine fcntl(2).
The freopen function may also fail and set errno for any of the errors specified for the routines open(2), fclose(3) and fflush(3).
CONFORMING TO
The fopen and freopen functions conform to ANSI X3.159-1989 (``ANSI C''). The fdopen function conforms to IEEE Std1003.1-1988 (``POSIX.1'').
SEE ALSO
open(2), fclose(3), fileno(3)

















