?
? ????? PHP ??? ???? ??? ?? ??
在頭文件<stdio.h>中定義 | ||
---|---|---|
(1) | ||
FILE * fopen(const char * filename,const char * mode); | (直到C99) | |
FILE * fopen(const char *限制文件名,const char *限制模式); | (自C99以來) | |
errno_t fopen_s(FILE * restrict *限制streamptr,const char *限制文件名,const char *限制模式); | (2) | (自C11以來) |
1)打開由該文件指示的文件filename
并返回指向與該文件關聯(lián)的文件流的指針。mode
用于確定文件訪問模式。
2)與(1)相同,除了寫入指向文件流的指針streamptr
以及在運行時檢測到以下錯誤并調(diào)用當前安裝的約束處理函數(shù):
streamptr
是一個空指針
filename
是一個空指針
mode
是一個空指針
作為所有邊界檢查函數(shù),fopen_s
只有在被__STDC_LIB_EXT1__
實現(xiàn)定義__STDC_WANT_LIB_EXT1__
并且1
在包含之前用戶定義為整數(shù)常量時才能保證可用<stdio.h>
。
文件名 | - | 將文件流關聯(lián)到的文件名 | |||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
模式 | - | 文件存取模式文件存取模式字符串含義說明如果文件已存在,則采取行動如果文件不存在則執(zhí)行“r”讀取打開讀取文件讀取從開始失敗讀取打開“w”寫入創(chuàng)建寫入文件破壞內(nèi)容創(chuàng)建新的“a”追加追加到文件寫入結(jié)束創(chuàng)建新的“r +”讀取擴展打開文件進行讀取/寫入從啟動錯誤讀取“w +”寫入擴展創(chuàng)建讀取/寫入文件銷毀內(nèi)容創(chuàng)建新的“ a +“append extended打開一個文件進行讀/寫寫操作以結(jié)束創(chuàng)建新的文件訪問模式標志”b“可以選擇性地指定在二進制模式下打開一個文件。該標志只對Windows系統(tǒng)有效。在附加文件訪問模式下,無論文件位置指示符的當前位置如何,數(shù)據(jù)都會寫入文件末尾。當以更新模式('+'作為上述模式參數(shù)值列表中的第二個或第三個字符)打開文件時,可以在關聯(lián)的流上執(zhí)行輸入和輸出。然而,輸出不應直接跟隨輸入,而不需要對fflush函數(shù)或文件定位函數(shù)(fseek,fsetpos或rewind)進行干預調(diào)用,并且輸入不應直接跟隨輸出,而不需要中間調(diào)用文件定位函數(shù),除非輸入操作遇到文件結(jié)束。在某些實現(xiàn)中打開(或創(chuàng)建)具有更新模式的文本文件可能會打開(或創(chuàng)建)二進制流。文件訪問模式標志“x” 可以選擇性地附加到“w”或“w +”說明符。如果文件存在,該標志強制該功能失敗,而不是覆蓋它。(C11)使用fopen_s或freopen_s時,用“w”或“a”創(chuàng)建的任何文件的文件訪問權限會阻止其他用戶訪問它。文件訪問模式標志“u”可以有選擇地添加到以“w”或“a”開頭的任何說明符中,以啟用默認的fopen權限。(C11)可以有選擇地添加到以“w”或“a”開頭的任何說明符中,以啟用默認的fopen權限。(C11)可以有選擇地添加到以“w”或“a”開頭的任何說明符中,以啟用默認的fopen權限。(C11) | File access mode string | Meaning | Explanation | Action if file already exists | Action if file does not exist | "r" | read | Open a file for reading | read from start | failure to open | "w" | write | Create a file for writing | destroy contents | create new | "a" | append | Append to a file | write to end | create new | "r+" | read extended | Open a file for read/write | read from start | error | "w+" | write extended | Create a file for read/write | destroy contents | create new | "a+" | append extended | Open a file for read/write | write to end | create new | File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has effect only on Windows systems. On the append file access modes, data is written to the end of the file regardless of the current position of the file position indicator. | When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations. | File access mode flag "x" can optionally be appended to "w" or "w+" specifiers. This flag forces the function to fail if the file exists, instead of overwriting it. (C11) | 當使用fopen_s或freopen_s時,用“w”或“a”創(chuàng)建的任何文件的文件訪問權限會阻止其他用戶訪問它。文件訪問模式標志“u”可以有選擇地添加到以“w”或“a”開頭的任何說明符中,以啟用默認的fopen權限。(C11) |
文件訪問模式字符串 | 含義 | 說明 | 行動如果文件已經(jīng)存在 | 如果文件不存在,則采取行動 | |||||||||||||||||||||||||||||||||||||
“R” | 讀 | 打開一個文件以供閱讀 | 從開始閱讀 | 未能打開 | |||||||||||||||||||||||||||||||||||||
“W” | 寫 | 創(chuàng)建一個文件寫入 | 破壞內(nèi)容 | 創(chuàng)建新的 | |||||||||||||||||||||||||||||||||||||
“一個” | 附加 | 附加到文件 | 寫入結(jié)束 | 創(chuàng)建新的 | |||||||||||||||||||||||||||||||||||||
“R +” | 閱讀擴展 | 打開文件進行讀取/寫入 | 從開始閱讀 | 錯誤 | |||||||||||||||||||||||||||||||||||||
“W +” | 寫入擴展 | 創(chuàng)建一個用于讀/寫的文件 | 破壞內(nèi)容 | 創(chuàng)建新的 | |||||||||||||||||||||||||||||||||||||
“A +” | 追加擴展 | 打開文件進行讀取/寫入 | 寫入結(jié)束 | 創(chuàng)建新的 |
| 可以選擇指定文件訪問模式標志“b”以二進制模式打開文件。該標志只對Windows系統(tǒng)有效。在附加文件訪問模式下,無論文件位置指示符的當前位置如何,數(shù)據(jù)都會寫入文件末尾。|
| 當以更新模式('+'作為上述模式參數(shù)值列表中的第二個或第三個字符)打開文件時,可以在關聯(lián)的流上執(zhí)行輸入和輸出。然而,輸出不應直接跟隨輸入而沒有插入調(diào)用fflush函數(shù)或文件定位函數(shù)(fseek,fsetpos,或倒帶),并且輸入不應直接跟隨輸出而沒有中間呼叫到一個文件中的定位函數(shù),除非輸入操作遇到文件結(jié)束。在某些實現(xiàn)中打開(或創(chuàng)建)具有更新模式的文本文件可能會打開(或創(chuàng)建)二進制流。|
| 文件訪問模式標志“x”可以選擇性地附加到“w”或“w +”說明符。如果文件存在,該標志強制該功能失敗,而不是覆蓋它。(C11)|
| 當使用fopen_s或freopen_s時,用“w”或“a”創(chuàng)建的任何文件的文件訪問權限會阻止其他用戶訪問它。文件訪問模式標志“u”可以有選擇地添加到以“w”或“a”開頭的任何說明符中,以啟用默認的fopen權限。(C11)|
| streamptr | - | 指向函數(shù)存儲結(jié)果的指針(out-parameter)|的指針
1)如果成功,返回一個指向新文件流的指針。除非filename
指向交互式設備,否則該流完全緩沖。出錯時,返回一個null pointer
。POSIX需要的是errno
在這種情況下設置。
2)如果成功,則返回零,并寫入指向新文件流的指針*streamptr
。出錯時,返回一個非零的錯誤代碼并將空指針寫入*streamptr
(除非streamptr
是空指針本身)。
格式filename
是實現(xiàn)定義的,并不一定是指文件(例如它可能是控制臺或通過文件系統(tǒng)API可訪問的其他設備)。在支持它們的平臺上,filename
可能包括絕對或相對的文件系統(tǒng)路徑。
#include <stdio.h>#include <stdlib.h> int main(void){ FILE* fp = fopen("test.txt", "r"); if(!fp) { perror("File opening failed"); return EXIT_FAILURE; } int c; // note: int, not char, required to handle EOF while ((c = fgetc(fp)) != EOF) { // standard C I/O file reading loop putchar(c); } if (ferror(fp)) puts("I/O error when reading"); else if (feof(fp)) puts("End of file reached successfully"); fclose(fp);}
C11標準(ISO / IEC 9899:2011):
7.21.5.3 fopen函數(shù)(p:305-306)
K.3.5.2.1 fopen_s函數(shù)(p:588-590)
C99標準(ISO / IEC 9899:1999):
7.19.5.3 fopen函數(shù)(p:271-272)
C89 / C90標準(ISO / IEC 9899:1990):
4.9.5.3 fopen函數(shù)