?
This document uses PHP Chinese website manual Release
在頭文件<stdio.h>中定義 | ||
---|---|---|
(1) | ||
FILE * freopen(const char * filename,const char * mode,F(xiàn)ILE * stream); | (直到C99) | |
FILE * freopen(const char *限制文件名,const char *限制模式,F(xiàn)ILE *限制流); | (自C99以來(lái)) | |
errno_t freopen_s(FILE * restrict * restrict newstreamptr,const char * restrict filename,const char * restrict mode,F(xiàn)ILE * restrict stream); | (2) | (自C11以來(lái)) |
1)首先,試圖關(guān)閉與之相關(guān)的文件stream
,忽略任何錯(cuò)誤。然后,如果filename
不為null,則嘗試打開(kāi)通過(guò)filename
使用mode
as 指定的文件fopen
,并將該文件與指向的文件流相關(guān)聯(lián)stream
。如果filename
是空指針,那么函數(shù)將嘗試重新打開(kāi)已經(jīng)關(guān)聯(lián)的文件stream
(在此情況下,它是實(shí)現(xiàn)定義允許哪些模式更改)。
2)與(1)相同,不同之處在于mode
,處理方式如下,fopen_s
指向文件流的指針被寫(xiě)入,newstreamptr
并且在運(yùn)行時(shí)檢測(cè)到以下錯(cuò)誤并調(diào)用當(dāng)前安裝的約束處理函數(shù):
newstreamptr
是一個(gè)空指針
stream
是一個(gè)空指針
mode
是一個(gè)空指針
作為所有的邊界檢查函數(shù),freopen_s
只能保證__STDC_LIB_EXT1__
是由實(shí)現(xiàn)定義的,并且如果用戶在包含之前定義__STDC_WANT_LIB_EXT1__
為整數(shù)常量。 1<stdio.h>
文件名 | - | 將文件流關(guān)聯(lián)到的文件名 | |||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
模式 | - | null-terminated character string determining new file access mode 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) When using fopen_s or freopen_s, file access permissions for any file created with "w" or "a" prevents other users from accessing it. File access mode flag "u" can optionally be prepended to any specifier that begins with "w" or "a", to enable the default fopen permissions. (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) | 當(dāng)使用fopen_s或freopen_s時(shí),用“w”或“a”創(chuàng)建的任何文件的文件訪問(wèn)權(quán)限會(huì)阻止其他用戶訪問(wèn)它。文件訪問(wèn)模式標(biāo)志“u”可以有選擇地添加到以“w”或“a”開(kāi)頭的任何說(shuō)明符中,以啟用默認(rèn)的fopen權(quán)限。(C11) |
文件訪問(wèn)模式字符串 | 含義 | 說(shuō)明 | 行動(dòng)如果文件已經(jīng)存在 | 如果文件不存在,則采取行動(dòng) | |||||||||||||||||||||||||||||||||||||
“R” | 讀 | 打開(kāi)一個(gè)文件以供閱讀 | 從開(kāi)始閱讀 | 未能打開(kāi) | |||||||||||||||||||||||||||||||||||||
“W” | 寫(xiě) | 創(chuàng)建一個(gè)文件寫(xiě)入 | 破壞內(nèi)容 | 創(chuàng)建新的 | |||||||||||||||||||||||||||||||||||||
“一個(gè)” | 附加 | 附加到文件 | 寫(xiě)入結(jié)束 | 創(chuàng)建新的 | |||||||||||||||||||||||||||||||||||||
“R +” | 閱讀擴(kuò)展 | 打開(kāi)文件進(jìn)行讀取/寫(xiě)入 | 從開(kāi)始閱讀 | 錯(cuò)誤 | |||||||||||||||||||||||||||||||||||||
“W +” | 寫(xiě)入擴(kuò)展 | 創(chuàng)建一個(gè)用于讀/寫(xiě)的文件 | 破壞內(nèi)容 | 創(chuàng)建新的 | |||||||||||||||||||||||||||||||||||||
“A +” | 追加擴(kuò)展 | 打開(kāi)文件進(jìn)行讀取/寫(xiě)入 | 寫(xiě)入結(jié)束 | 創(chuàng)建新的 |
1)stream
成功的值的副本,失敗時(shí)的空指針。
2)成功時(shí)為零(并且stream
寫(xiě)入的值的副本,*newstreamptr
錯(cuò)誤時(shí)不為零(并且空指針被寫(xiě)入,*newstreamptr
除非newstreamptr
它本身是空指針)。
freopen
是通過(guò)I / O操作或通過(guò)I / O操作建立后,改變流的窄/寬方向的唯一方法fwide
。
以下代碼重定向stdout
到一個(gè)文件。
#include <stdio.h>#include <stdlib.h> int main(void){ puts("stdout is printed to console"); if (freopen("redir.txt", "w", stdout) == NULL) { perror("freopen() failed"); return EXIT_FAILURE; } puts("stdout is redirected to a file"); // this is written to redir.txt fclose(stdout);}
輸出:
stdout is printed to console
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.21.5.4 freopen函數(shù)(p:307)
K.3.5.2.2 freopen_s函數(shù)(p:590)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.19.5.4 freopen函數(shù)(p:272-273)
C89 / C90標(biāo)準(zhǔn)(ISO / IEC 9899:1990):
4.9.5.4 freopen函數(shù)
fopenfopen_s(C11) | 打開(kāi)一個(gè)文件(函數(shù)) |
---|---|
FCLOSE | 關(guān)閉文件(函數(shù)) |
| 用于freopen的C ++文檔