.\" Copyright (c) 1990, 1991 The Regents of the University of California. .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" Chris Torek and the American National Standards Committee X3, .\" on Information Processing Systems. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)fopen.3 6.8 (Berkeley) 6/29/91 .\" .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu .\" Modified, aeb, 960421, 970806 .\" Modified, joey, aeb, 2002-01-03 .\" .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi .\" all rights reserved. .\" Translated Thu Feb 13 12:10:38 JST 1997 .\" by YOSHINO Takashi .\" Updated & Modified Mon Mar 1 1999 .\" by NAKANO Takeo .\" Updated 1999-12-08, Kentaro Shirakata .\" Updated 2001-12-13, Kentaro Shirakata .\" Updated 2002-03-23, Kentaro Shirakata .\" Updated 2005-10-15, Kentaro Shirakata .\" Updated 2006-07-20, Akihiro MOTOKI .\" .\"WORD: file descriptor ファイル記述子 .\"WORD: file position indicator ファイル位置指示子 .\"WORD: indicator 指示子 .\"WORD: global variable 大域変数 .\"WORD: cancellation points 取り消しポイント .\" .TH FOPEN 3 2009-02-23 "GNU" "Linux Programmer's Manual" .SH 名前 .\"O fopen, fdopen, freopen \- stream open functions fopen, fdopen, freopen \- ストリームを開く関数 .SH 書式 .nf .B #include .sp .BI "FILE *fopen(const char *" path ", const char *" mode ); .BI "FILE *fdopen(int " fd ", const char *" mode ); .BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream ); .fi .sp .in -4n .\"O Feature Test Macro Requirements for glibc (see .\"O .BR feature_test_macros (7)): glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (7) 参照): .in .sp .BR fdopen (): _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE .SH 説明 .\"O The .\"O .BR fopen () .\"O function opens the file whose name is the string pointed to by .\"O .I path .\"O and associates a stream with it. .BR fopen () 関数は、 .I path で指定された名前のファイルを開き、ストリームと結びつける。 .PP .\"O The argument .\"O .I mode .\"O points to a string beginning with one of the following sequences .\"O (Additional characters may follow these sequences.): 引数 .I mode は、以下に続く文字のひとつから始まる文字列へのポインタである (追加の文字がこの文字の後に続くこともある): .TP .B r .\"O Open text file for reading. .\"O The stream is positioned at the beginning of the file. テキストファイルを読み出すために開く。 ストリームはファイルの先頭に位置される。 .TP .B r+ .\"O Open for reading and writing. .\"O The stream is positioned at the beginning of the file. 読み出しおよび書き込みするために開く。 ストリームはファイルの先頭に位置される。 .TP .B w .\"O Truncate file to zero length or create text file for writing. .\"O The stream is positioned at the beginning of the file. ファイルを書き込みのために開く。 ファイルが既に存在する場合には長さゼロに切り詰める。 ファイルがなかった場合には新たに作成する。 ストリームはファイルの先頭に位置される。 .TP .B w+ .\"O Open for reading and writing. .\"O The file is created if it does not exist, otherwise it is truncated. .\"O The stream is positioned at the beginning of the file. 読み出しおよび書き込みのために開く。 ファイルが存在していない場合には新たに作成する。 存在している場合には長さゼロに切り詰められる。 ストリームはファイルの先頭に位置される。 .TP .B a .\"O Open for appending (writing at end of file). .\"O The file is created if it does not exist. .\"O The stream is positioned at the end of the file. 追加 (ファイルの最後に書き込む) のために開く。 ファイルが存在していない場合には新たに作成する。 ストリームはファイルの最後に位置される。 .TP .B a+ .\"O Open for reading and appending (writing at end of file). .\"O The file is created if it does not exist. .\"O The initial file position for reading is at the beginning of the file, .\"O but output is always appended to the end of the file. 読み出しおよび追加 (ファイルの最後に書き込む) のために開く。 ファイルが存在していない場合には新たに作成する。 読み出しの初期ファイル位置はファイルの先頭であるが、 書き込みは常にファイルの最後に追加される。 .PP .\"O The .\"O .I mode .\"O string can also include the letter \(aqb\(aq either as a last character or as .\"O a character between the characters in any of the two-character strings .\"O described above. .\"O This is strictly for compatibility with C89 .\"O and has no effect; the \(aqb\(aq is ignored on all POSIX .\"O conforming systems, including Linux. .I mode 文字列には文字 \(aqb\(aq を追加指定することができ、 .I mode 文字列の最後の文字として指定する。 上記のうち 2 文字のモードの場合には 2 つの文字の間に指定することもできる。 これは C89 との互換性のためだけに用意された ものであり、関数の実行に対してはいかなる影響も持たない。 すなわち、Linux を含む全ての POSIX 準拠システムでは、 この \(aqb\(aq は無視される。 .\"O (Other systems may treat text files and binary files differently, .\"O and adding the \(aqb\(aq may be a good idea if you do I/O to a binary .\"O file and expect that your program may be ported to non-UNIX .\"O environments.) (その他のシステムではテキストファイルとバイナリファイルを別々に扱うものもあるので、 もしバイナリファイルの入出力を行い、 そのプログラムが非 UNIX 環境へ移植されると予測するなら、 \(aqb\(aqを付けておくのは良い考えである) .PP .\"O See NOTES below for details of glibc extensions for .\"O .IR mode . .I mode の glibc による拡張の詳細については下記の「注意」を参照。 .PP .\"O Any created files will have mode .\"O .BR S_IRUSR " | " S_IWUSR " | " S_IRGRP " | " S_IWGRP " | " S_IROTH " | " S_IWOTH .\"O (0666), as modified by the process's umask value (see .\"O .BR umask (2)). すべての生成されたファイルは、 .BR S_IRUSR " | " S_IWUSR " | " S_IRGRP " | " S_IWGRP " | " S_IROTH " | " S_IWOTH (0666) のモードを そのプロセスの umask 値によって修正したモードを持つ .RB ( umask (2) を見よ)。 .PP .\"O Reads and writes may be intermixed on read/write streams in any order. .\"O Note that ANSI C requires that a file positioning function intervene .\"O between output and input, unless an input operation encounters end-of-file. .\"O (If this condition is not met, then a read is allowed to return the .\"O result of writes other than the most recent.) .\"O Therefore it is good practice (and indeed sometimes necessary .\"O under Linux) to put an .\"O .BR fseek (3) .\"O or .\"O .BR fgetpos (3) .\"O operation between write and read operations on such a stream. .\"O This operation may be an apparent no-op .\"O (as in \fIfseek(..., 0L, SEEK_CUR)\fP .\"O called for its synchronizing side effect. 読み出し/書き込みストリームに対しては任意の順序で読み書きを行うことができる。 ただし ANSI C では、 (入力操作がファイルの末尾に到達した場合を除いて) 出力と入力の間にはファイルの位置決め関数を 挟まなければならないことになっていることに注意されたい (この条件を満足しない場合には、読み込み操作は、 最後に書き込まれたものでなく、以前に書き込まれた 値を返すことを許されている)。 したがって、このようなストリームでの読み書き操作の間には .BR fseek (3) または .BR fgetpos (3) 操作を挟んでおくと良いだろう (Linux では本当に必要となることもときどきある)。 この操作は見かけ上何もしない操作 (no-op) でも良い (例えば \fIfseek(..., 0L, SEEK_CUR)\fP を その副次的効果である同期のためだけに呼べば良い)。 .PP .\"O Opening a file in append mode (\fBa\fP as the first character of .\"O .IR mode ) .\"O causes all subsequent write operations to this stream to occur .\"O at end-of-file, as if preceded by an .\"O .nf .\"O .\"O fseek(stream,0,SEEK_END); .\"O .fi .\"O .PP .\"O call. ファイルを追加モード .RI ( mode の最初の文字を \fBa\fP にする) で開くと、 このストリームに対する書き込み操作は (先に .nf fseek(stream,0,SEEK_END); .fi の呼び出しを実行したかのように) ファイル末尾に対して行われる。 .PP .\"O The .\"O .BR fdopen () .\"O function associates a stream with the existing file descriptor, .\"O .IR fd . .BR fdopen () 関数は、既存のファイル記述子 .I fd にストリームを結びつける。 .\"O The .\"O .I mode .\"O of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") .\"O must be compatible with the mode of the file descriptor. ストリームの .I mode ("r", "r+", "w", "w+", "a", "a+" のいずれか) は ファイル記述子のモードと互換のものでなければならない。 .\"O The file position indicator of the new stream is set to that .\"O belonging to .\"O .IR fd , .\"O and the error and end-of-file indicators are cleared. .\"O Modes "w" or "w+" do not cause truncation of the file. 新しいストリームのファイル位置指示子は .I fd に属している値に設定される。 error と end-of-file の各指示子はクリアされる。 "w" および "w+" モードでのファイルの切り詰めは行われない。 .\"O The file descriptor is not dup'ed, and will be closed when .\"O the stream created by .\"O .BR fdopen () .\"O is closed. .\"O The result of applying .\"O .BR fdopen () .\"O to a shared memory object is undefined. ファイル記述子の複製は行なわれない。 .BR fdopen () で作成されたストリームが閉じられたときにファイル記述子も 閉じられる。 共有メモリのオブジェクトへ .BR fdopen () を行ったときの結果は定義されていない。 .PP .\"O The .\"O .BR freopen () .\"O function opens the file whose name is the string pointed to by .\"O .I path .\"O and associates the stream pointed to by .\"O .I stream .\"O with it. .\"O The original stream (if it exists) is closed. .\"O The .\"O .I mode .\"O argument is used just as in the .\"O .BR fopen () .\"O function. .\"O The primary use of the .\"O .BR freopen () .\"O function is to change the file associated with a standard text stream .\"O .RI ( stderr ", " stdin ", or " stdout ). .BR freopen () 関数は .I path で名前が指定されたファイルを開き、 .I stream で指定されたストリームに、そのファイルを結びつける。 もとのストリームは (もし存在する場合には) 閉じられる。 .I mode 引数は .BR fopen () 関数と同じ形で使われる。 .BR freopen () 関数の主な用途は、標準テキストストリーム .RI ( stderr ", " stdin ", " stdout ) と対応付けられているファイルを変更することである。 .SH 返り値 .\"O Upon successful completion .\"O .BR fopen (), .\"O .BR fdopen () .\"O and .\"O .BR freopen () .\"O return a .\"O .I FILE .\"O pointer. .\"O Otherwise, NULL is returned and .\"O .I errno .\"O is set to indicate the error. .BR fopen (), .BR fdopen (), .BR freopen () は成功すると .I FILE 型のポインタを返す。 失敗すると NULL が返され、 .I errno がエラーを示す値にセットされる。 .SH エラー .TP .B EINVAL .\"O The .\"O .I mode .\"O provided to .\"O .BR fopen (), .\"O .BR fdopen (), .\"O or .\"O .BR freopen () .\"O was invalid. .BR fopen (), .BR fdopen (), .BR freopen () で与えられた .I mode が不適切である。 .PP .\"O The .\"O .BR fopen (), .\"O .BR fdopen () .\"O and .\"O .BR freopen () .\"O functions may also fail and set .\"O .I errno .\"O for any of the errors specified for the routine .\"O .BR malloc (3). .BR fopen (), .BR fdopen (), .BR freopen () 関数は .BR malloc (3) ルーチンで規定されているエラーでも失敗することがあり、 その時は対応する値に .I errno をセットする。 .PP .\"O The .\"O .BR fopen () .\"O function may also fail and set .\"O .I errno .\"O for any of the errors specified for the routine .\"O .BR open (2). .BR fopen () 関数は .BR open (2) ルーチンで規定されているエラーでも失敗することがあり、 その時は対応する値に .I errno をセットする。 .PP .\"O The .\"O .BR fdopen () .\"O function may also fail and set .\"O .I errno .\"O for any of the errors specified for the routine .\"O .BR fcntl (2). .BR fdopen () 関数は .BR fcntl (2) ルーチンで規定されているエラーでも失敗することがあり、 その時は対応する値に .I errno をセットする。 .PP .\"O The .\"O .BR freopen () .\"O function may also fail and set .\"O .I errno .\"O for any of the errors specified for the routines .\"O .BR open (2), .\"O .BR fclose (3) .\"O and .\"O .BR fflush (3). .BR freopen () 関数は .BR open (2), .BR fclose (3), .BR fflush (3) 各ルーチンで規定されているエラーでも失敗することがあり、 その時は対応する値に .I errno をセットする。 .SH 準拠 .\"O The .\"O .BR fopen () .\"O and .\"O .BR freopen () .\"O functions conform to C89. .\"O The .\"O .BR fdopen () .\"O function conforms to POSIX.1-1990. .BR fopen () 関数と .BR freopen () 関数は C89に準拠している。 .BR fdopen () 関数は POSIX.1-1990 に準拠している。 .\"O .SH NOTES .\"O .SS Glibc Notes .SH 注意 .SS glibc での注意 .\"O The GNU C library allows the following extensions for the string specified in .\"O .IR mode : GNU C ライブラリでは、 .I mode に指定できる文字列として、以下の拡張が行われている: .TP .\"O .BR c " (since glibc 2.3.3)" .BR c " (glibc 2.3.3 以降)" .\"O Do not make the open operation, .\"O or subsequent read and write operations, .\"O thread cancellation points. open 操作、それに続く read/write 操作の、 スレッドの取り消しポイント (cancellation points) を作成しない。 .TP .\"O .BR e " (since glibc 2.7)" .BR e " (glibc 2.7 以降)" .\"O Open the file with the .\"O .B O_CLOEXEC .\"O flag. .\"O See .\"O .BR open (2) .\"O for more information. .B O_CLOEXEC フラグを有効にしてファイルをオープンする。 詳細は .BR open (2) を参照。 .TP .\"O .BR m " (since glibc 2.3)" .BR m " (glibc 2.3 以降)" .\"O Attempt to access the file using .\"O .BR mmap (2), .\"O rather than I/O system calls .\"O .RB ( read (2), .\"O .BR write (2)). .\"O Currently, .\"O .\" As at glibc 2.4: .\"O use of .\"O .BR mmap (2) .\"O is only attempted for a file opened for reading. I/O システムコール .RB ( read (2), .BR write (2)) ではなく、 .BR mmap (2) を使ってファイルにアクセスしようとする。 .\" glibc 2.4 では .BR mmap (2) を使おうとするのは、読み出し用にオープンするファイルについてだけである。 .TP .B x .\"O Open the file exclusively .\"O (like the .\"O .B O_EXCL .\"O flag of .\"O .BR open (2)). .\"O If the file already exists, .\"O .BR fopen () .\"O fails, and sets .\"O .I errno .\"O to .\"O .BR EEXIST . ファイルを排他的にオープンする .RB ( open (2) の .B O_EXCL フラグと同様)。 ファイルがすでに存在する場合、 .BR fopen () は失敗し、 .I errno に .B EEXIST がセットされる。 .\"O This flag is ignored for .\"O .BR fdopen (). このフラグは .BR fdopen () では無視される。 .\" FIXME document /,ccs= charset/ .\"O .SH "SEE ALSO" .SH 関連項目 .BR open (2), .BR fclose (3), .BR fileno (3), .BR fmemopen (3), .BR fopencookie (3)