freopen_s
stdio.h:インデックス への移動
ヘッダー ファイル
stdio.h
カテゴリ
ファイル アクセス関数
プロトタイプ
errno_t freopen_s(FILE * restrict * restrict newstreamptr, const char * restrict filename, const char * restrict mode, FILE * restrict stream);
説明
freopen の代わりとなるもので、セキュリティ機能が強化されています。
freopen_s は、開かれているストリームを指定ファイルに切り替えます。 正常に開くことができたかどうかにかかわらず、ストリームを閉じます。freopen_s は、stdin、stdout、stderr に関連付けられているファイルを変更する場合に役立ちます。 それには、まず stream パラメータに関連付けられている任意のファイルを閉じた後、filename で指定されたファイルを開き、stream パラメータで指定されたストリームをそのファイルに関連付けます。
ファイルが正常に開かれた場合、そのファイルのファイル ポインタ値が newstreamptr パラメータに設定されます。 そうでない場合、newstreamptr は NULL ポインタになります。
文字列 mode には、freopen の場合と同じ値を同じ意味で指定できます。
アクセス違反が発生した場合、freopen_s はファイルを閉じず、ファイルを開こうともしません。
戻り値
正常に終了した場合、freopen_s はゼロを返し、そうでない場合はゼロでない値を返します。
例
#include <stdio.h>
int main(void)
{
/* Redirect stdout */
FILE *file;
file = new FILE;
if (freopen_s(&file,"OUTPUT.FIL", "w", stdout)){
fprintf(stderr, "error redirecting stdout\n");
exit(1);
}
/* This output will go to a file */
printf("This will go into a file.");
/* Close the standard output stream */
fclose(stdout);
return 0;
}
移植性
| POSIX | Win32 | ANSI C | ANSI C++ | |
|---|---|---|---|---|
|
freopen_s |
+ |
+ |
+ |
+ |