OSDN Git Service

936ed40a865746146332c26303cc19b5d3bc8104
[linuxjm/LDP_man-pages.git] / draft / man3 / fopen.3
1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" Chris Torek and the American National Standards Committee X3,
6 .\" on Information Processing Systems.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\"    must display the following acknowledgement:
18 .\"     This product includes software developed by the University of
19 .\"     California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\"    may be used to endorse or promote products derived from this software
22 .\"    without specific prior written permission.
23 .\"
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" SUCH DAMAGE.
35 .\"
36 .\"     @(#)fopen.3     6.8 (Berkeley) 6/29/91
37 .\"
38 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
39 .\" Modified, aeb, 960421, 970806
40 .\" Modified, joey, aeb, 2002-01-03
41 .\"
42 .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi
43 .\"       all rights reserved.
44 .\" Translated Thu Feb 13 12:10:38 JST 1997
45 .\"       by YOSHINO Takashi <yoshino@civil.jcn.nihon-u.ac.jp>
46 .\" Updated & Modified Mon Mar 1 1999
47 .\"       by NAKANO Takeo <nakano@apm.seikei.ac.jp>
48 .\" Updated 1999-12-08, Kentaro Shirakata <argrath@ub32.org>
49 .\" Updated 2001-12-13, Kentaro Shirakata <argrath@ub32.org>
50 .\" Updated 2002-03-23, Kentaro Shirakata <argrath@ub32.org>
51 .\" Updated 2005-10-15, Kentaro Shirakata <argrath@ub32.org>
52 .\" Updated 2006-07-20, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
53 .\"
54 .\"WORD:        file descriptor         ファイル記述子
55 .\"WORD:        file position indicator ファイル位置指示子
56 .\"WORD:        indicator               指示子
57 .\"WORD:        global variable         大域変数
58 .\"WORD:        cancellation points     取り消しポイント
59 .\"
60 .TH FOPEN 3  2009-02-23 "GNU" "Linux Programmer's Manual"
61 .SH 名前
62 .\"O fopen, fdopen, freopen \- stream open functions
63 fopen, fdopen, freopen \- ストリームを開く関数
64 .SH 書式
65 .nf
66 .B #include <stdio.h>
67 .sp
68 .BI "FILE *fopen(const char *" path ", const char *" mode );
69
70 .BI "FILE *fdopen(int " fd ", const char *" mode );
71
72 .BI "FILE *freopen(const char *" path ", const char *" mode ", FILE *" stream );
73 .fi
74 .sp
75 .in -4n
76 .\"O Feature Test Macro Requirements for glibc (see
77 .\"O .BR feature_test_macros (7)):
78 glibc 向けの機能検査マクロの要件
79 .RB ( feature_test_macros (7)
80 参照):
81 .in
82 .sp
83 .BR fdopen ():
84 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _POSIX_SOURCE
85 .SH 説明
86 .\"O The
87 .\"O .BR fopen ()
88 .\"O function opens the file whose name is the string pointed to by
89 .\"O .I path
90 .\"O and associates a stream with it.
91 .BR fopen ()
92 関数は、
93 .I path
94 で指定された名前のファイルを開き、ストリームと結びつける。
95 .PP
96 .\"O The argument
97 .\"O .I mode
98 .\"O points to a string beginning with one of the following sequences
99 .\"O (Additional characters may follow these sequences.):
100 引数
101 .I mode
102 は、以下に続く文字のひとつから始まる文字列へのポインタである
103 (追加の文字がこの文字の後に続くこともある):
104 .TP
105 .B r
106 .\"O Open text file for reading.
107 .\"O The stream is positioned at the beginning of the file.
108 テキストファイルを読み出すために開く。
109 ストリームはファイルの先頭に位置される。
110 .TP
111 .B r+
112 .\"O Open for reading and writing.
113 .\"O The stream is positioned at the beginning of the file.
114 読み出しおよび書き込みするために開く。
115 ストリームはファイルの先頭に位置される。
116 .TP
117 .B w
118 .\"O Truncate file to zero length or create text file for writing.
119 .\"O The stream is positioned at the beginning of the file.
120 ファイルを書き込みのために開く。
121 ファイルが既に存在する場合には長さゼロに切り詰める。
122 ファイルがなかった場合には新たに作成する。
123 ストリームはファイルの先頭に位置される。
124 .TP
125 .B w+
126 .\"O Open for reading and writing.
127 .\"O The file is created if it does not exist, otherwise it is truncated.
128 .\"O The stream is positioned at the beginning of the file.
129 読み出しおよび書き込みのために開く。
130 ファイルが存在していない場合には新たに作成する。
131 存在している場合には長さゼロに切り詰められる。
132 ストリームはファイルの先頭に位置される。
133 .TP
134 .B a
135 .\"O Open for appending (writing at end of file).
136 .\"O The file is created if it does not exist.
137 .\"O The stream is positioned at the end of the file.
138 追加 (ファイルの最後に書き込む) のために開く。
139 ファイルが存在していない場合には新たに作成する。
140 ストリームはファイルの最後に位置される。
141 .TP
142 .B a+
143 .\"O Open for reading and appending (writing at end of file).
144 .\"O The file is created if it does not exist.
145 .\"O The initial file position for reading is at the beginning of the file,
146 .\"O but output is always appended to the end of the file.
147 読み出しおよび追加 (ファイルの最後に書き込む) のために開く。
148 ファイルが存在していない場合には新たに作成する。
149 読み出しの初期ファイル位置はファイルの先頭であるが、
150 書き込みは常にファイルの最後に追加される。
151 .PP
152 .\"O The
153 .\"O .I mode
154 .\"O string can also include the letter \(aqb\(aq either as a last character or as
155 .\"O a character between the characters in any of the two-character strings
156 .\"O described above.
157 .\"O This is strictly for compatibility with C89
158 .\"O and has no effect; the \(aqb\(aq is ignored on all POSIX
159 .\"O conforming systems, including Linux.
160 .I mode
161 文字列には文字 \(aqb\(aq を追加指定することができ、
162 .I mode
163 文字列の最後の文字として指定する。
164 上記のうち 2 文字のモードの場合には 2 つの文字の間に指定することもできる。
165 これは C89 との互換性のためだけに用意された
166 ものであり、関数の実行に対してはいかなる影響も持たない。
167 すなわち、Linux を含む全ての POSIX 準拠システムでは、
168 この \(aqb\(aq は無視される。
169 .\"O (Other systems may treat text files and binary files differently,
170 .\"O and adding the \(aqb\(aq may be a good idea if you do I/O to a binary
171 .\"O file and expect that your program may be ported to non-UNIX
172 .\"O environments.)
173 (その他のシステムではテキストファイルとバイナリファイルを別々に扱うものもあるので、
174 もしバイナリファイルの入出力を行い、
175 そのプログラムが非 UNIX 環境へ移植されると予測するなら、
176 \(aqb\(aqを付けておくのは良い考えである)
177 .PP
178 .\"O See NOTES below for details of glibc extensions for
179 .\"O .IR mode .
180 .I mode
181 の glibc による拡張の詳細については下記の「注意」を参照。
182 .PP
183 .\"O Any created files will have mode
184 .\"O .BR S_IRUSR " | " S_IWUSR " | "  S_IRGRP " | "  S_IWGRP " | " S_IROTH " | " S_IWOTH
185 .\"O (0666), as modified by the process's umask value (see
186 .\"O .BR umask (2)).
187 すべての生成されたファイルは、
188 .BR S_IRUSR " | " S_IWUSR " | "  S_IRGRP " | "  S_IWGRP " | " S_IROTH " | " S_IWOTH
189 (0666) のモードを
190 そのプロセスの umask 値によって修正したモードを持つ
191 .RB ( umask (2)
192 を見よ)。
193 .PP
194 .\"O Reads and writes may be intermixed on read/write streams in any order.
195 .\"O Note that ANSI C requires that a file positioning function intervene
196 .\"O between output and input, unless an input operation encounters end-of-file.
197 .\"O (If this condition is not met, then a read is allowed to return the
198 .\"O result of writes other than the most recent.)
199 .\"O Therefore it is good practice (and indeed sometimes necessary
200 .\"O under Linux) to put an
201 .\"O .BR fseek (3)
202 .\"O or
203 .\"O .BR fgetpos (3)
204 .\"O operation between write and read operations on such a stream.
205 .\"O This operation may be an apparent no-op
206 .\"O (as in \fIfseek(..., 0L, SEEK_CUR)\fP
207 .\"O called for its synchronizing side effect.
208 読み出し/書き込みストリームに対しては任意の順序で読み書きを行うことができる。
209 ただし ANSI C では、
210 (入力操作がファイルの末尾に到達した場合を除いて)
211 出力と入力の間にはファイルの位置決め関数を
212 挟まなければならないことになっていることに注意されたい
213 (この条件を満足しない場合には、読み込み操作は、
214 最後に書き込まれたものでなく、以前に書き込まれた
215 値を返すことを許されている)。
216 したがって、このようなストリームでの読み書き操作の間には
217 .BR fseek (3)
218 または
219 .BR fgetpos (3)
220 操作を挟んでおくと良いだろう
221 (Linux では本当に必要となることもときどきある)。
222 この操作は見かけ上何もしない操作 (no-op) でも良い
223 (例えば \fIfseek(..., 0L, SEEK_CUR)\fP を
224 その副次的効果である同期のためだけに呼べば良い)。
225 .PP
226 .\"O Opening a file in append mode (\fBa\fP as the first character of
227 .\"O .IR mode )
228 .\"O causes all subsequent write operations to this stream to occur
229 .\"O at end-of-file, as if preceded by an
230 .\"O .nf
231 .\"O 
232 .\"O     fseek(stream,0,SEEK_END);
233 .\"O .fi
234 .\"O .PP
235 .\"O call.
236 ファイルを追加モード
237 .RI ( mode
238 の最初の文字を \fBa\fP にする) で開くと、
239 このストリームに対する書き込み操作は (先に
240 .nf
241
242     fseek(stream,0,SEEK_END);
243
244 .fi
245 の呼び出しを実行したかのように) ファイル末尾に対して行われる。
246 .PP
247 .\"O The
248 .\"O .BR fdopen ()
249 .\"O function associates a stream with the existing file descriptor,
250 .\"O .IR fd .
251 .BR fdopen ()
252 関数は、既存のファイル記述子
253 .I fd
254 にストリームを結びつける。
255 .\"O The
256 .\"O .I mode
257 .\"O of the stream (one of the values "r", "r+", "w", "w+", "a", "a+")
258 .\"O must be compatible with the mode of the file descriptor.
259 ストリームの
260 .I mode
261 ("r", "r+", "w", "w+", "a", "a+" のいずれか) は
262 ファイル記述子のモードと互換のものでなければならない。
263 .\"O The file position indicator of the new stream is set to that
264 .\"O belonging to
265 .\"O .IR fd ,
266 .\"O and the error and end-of-file indicators are cleared.
267 .\"O Modes "w" or "w+" do not cause truncation of the file.
268 新しいストリームのファイル位置指示子は
269 .I fd
270 に属している値に設定される。
271 error と end-of-file の各指示子はクリアされる。
272 "w" および "w+" モードでのファイルの切り詰めは行われない。
273 .\"O The file descriptor is not dup'ed, and will be closed when
274 .\"O the stream created by
275 .\"O .BR fdopen ()
276 .\"O is closed.
277 .\"O The result of applying
278 .\"O .BR fdopen ()
279 .\"O to a shared memory object is undefined.
280 ファイル記述子の複製は行なわれない。
281 .BR fdopen ()
282 で作成されたストリームが閉じられたときにファイル記述子も
283 閉じられる。
284 共有メモリのオブジェクトへ
285 .BR fdopen ()
286 を行ったときの結果は定義されていない。
287 .PP
288 .\"O The
289 .\"O .BR freopen ()
290 .\"O function opens the file whose name is the string pointed to by
291 .\"O .I path
292 .\"O and associates the stream pointed to by
293 .\"O .I stream
294 .\"O with it.
295 .\"O The original stream (if it exists) is closed.
296 .\"O The
297 .\"O .I mode
298 .\"O argument is used just as in the
299 .\"O .BR fopen ()
300 .\"O function.
301 .\"O The primary use of the
302 .\"O .BR freopen ()
303 .\"O function is to change the file associated with a standard text stream
304 .\"O .RI ( stderr ", " stdin ", or " stdout ).
305 .BR freopen ()
306 関数は
307 .I path
308 で名前が指定されたファイルを開き、
309 .I stream
310 で指定されたストリームに、そのファイルを結びつける。
311 もとのストリームは (もし存在する場合には) 閉じられる。
312 .I mode
313 引数は
314 .BR fopen ()
315 関数と同じ形で使われる。
316 .BR freopen ()
317 関数の主な用途は、標準テキストストリーム
318 .RI ( stderr ", " stdin ", " stdout )
319 と対応付けられているファイルを変更することである。
320 .SH 返り値
321 .\"O Upon successful completion
322 .\"O .BR fopen (),
323 .\"O .BR fdopen ()
324 .\"O and
325 .\"O .BR freopen ()
326 .\"O return a
327 .\"O .I FILE
328 .\"O pointer.
329 .\"O Otherwise, NULL is returned and
330 .\"O .I errno
331 .\"O is set to indicate the error.
332 .BR fopen (),
333 .BR fdopen (),
334 .BR freopen ()
335 は成功すると
336 .I FILE
337 型のポインタを返す。
338 失敗すると NULL が返され、
339 .I errno
340 がエラーを示す値にセットされる。
341 .SH エラー
342 .TP
343 .B EINVAL
344 .\"O The
345 .\"O .I mode
346 .\"O provided to
347 .\"O .BR fopen (),
348 .\"O .BR fdopen (),
349 .\"O or
350 .\"O .BR freopen ()
351 .\"O was invalid.
352 .BR fopen (),
353 .BR fdopen (),
354 .BR freopen ()
355 で与えられた
356 .I mode
357 が不適切である。
358 .PP
359 .\"O The
360 .\"O .BR fopen (),
361 .\"O .BR fdopen ()
362 .\"O and
363 .\"O .BR freopen ()
364 .\"O functions may also fail and set
365 .\"O .I errno
366 .\"O for any of the errors specified for the routine
367 .\"O .BR malloc (3).
368 .BR fopen (),
369 .BR fdopen (),
370 .BR freopen ()
371 関数は
372 .BR malloc (3)
373 ルーチンで規定されているエラーでも失敗することがあり、
374 その時は対応する値に
375 .I errno
376 をセットする。
377 .PP
378 .\"O The
379 .\"O .BR fopen ()
380 .\"O function may also fail and set
381 .\"O .I errno
382 .\"O for any of the errors specified for the routine
383 .\"O .BR open (2).
384 .BR fopen ()
385 関数は
386 .BR open (2)
387 ルーチンで規定されているエラーでも失敗することがあり、
388 その時は対応する値に
389 .I errno
390 をセットする。
391 .PP
392 .\"O The
393 .\"O .BR fdopen ()
394 .\"O function may also fail and set
395 .\"O .I errno
396 .\"O for any of the errors specified for the routine
397 .\"O .BR fcntl (2).
398 .BR fdopen ()
399 関数は
400 .BR fcntl (2)
401 ルーチンで規定されているエラーでも失敗することがあり、
402 その時は対応する値に
403 .I errno
404 をセットする。
405 .PP
406 .\"O The
407 .\"O .BR freopen ()
408 .\"O function may also fail and set
409 .\"O .I errno
410 .\"O for any of the errors specified for the routines
411 .\"O .BR open (2),
412 .\"O .BR fclose (3)
413 .\"O and
414 .\"O .BR fflush (3).
415 .BR freopen ()
416 関数は
417 .BR open (2),
418 .BR fclose (3),
419 .BR fflush (3)
420 各ルーチンで規定されているエラーでも失敗することがあり、
421 その時は対応する値に
422 .I errno
423 をセットする。
424 .SH 準拠
425 .\"O The
426 .\"O .BR fopen ()
427 .\"O and
428 .\"O .BR freopen ()
429 .\"O functions conform to C89.
430 .\"O The
431 .\"O .BR fdopen ()
432 .\"O function conforms to POSIX.1-1990.
433 .BR fopen ()
434 関数と
435 .BR freopen ()
436 関数は C89に準拠している。
437 .BR fdopen ()
438 関数は POSIX.1-1990 に準拠している。
439 .\"O .SH NOTES
440 .\"O .SS Glibc Notes
441 .SH 注意
442 .SS glibc での注意
443 .\"O The GNU C library allows the following extensions for the string specified in
444 .\"O .IR mode :
445 GNU C ライブラリでは、
446 .I mode
447 に指定できる文字列として、以下の拡張が行われている:
448 .TP
449 .\"O .BR c " (since glibc 2.3.3)"
450 .BR c " (glibc 2.3.3 以降)"
451 .\"O Do not make the open operation,
452 .\"O or subsequent read and write operations,
453 .\"O thread cancellation points.
454 open 操作、それに続く read/write 操作の、
455 スレッドの取り消しポイント (cancellation points)
456 を作成しない。
457 .TP
458 .\"O .BR e " (since glibc 2.7)"
459 .BR e " (glibc 2.7 以降)"
460 .\"O Open the file with the
461 .\"O .B O_CLOEXEC
462 .\"O flag.
463 .\"O See
464 .\"O .BR open (2)
465 .\"O for more information.
466 .B O_CLOEXEC
467 フラグを有効にしてファイルをオープンする。
468 詳細は
469 .BR open (2)
470 を参照。
471 .TP
472 .\"O .BR m " (since glibc 2.3)"
473 .BR m " (glibc 2.3 以降)"
474 .\"O Attempt to access the file using
475 .\"O .BR mmap (2),
476 .\"O rather than I/O system calls
477 .\"O .RB ( read (2),
478 .\"O .BR write (2)).
479 .\"O Currently,
480 .\"O .\" As at glibc 2.4:
481 .\"O use of
482 .\"O .BR mmap (2)
483 .\"O is only attempted for a file opened for reading.
484 I/O システムコール
485 .RB ( read (2),
486 .BR write (2))
487 ではなく、
488 .BR mmap (2)
489 を使ってファイルにアクセスしようとする。
490 .\" glibc 2.4 では
491 .BR mmap (2)
492 を使おうとするのは、読み出し用にオープンするファイルについてだけである。
493 .TP
494 .B x
495 .\"O Open the file exclusively
496 .\"O (like the
497 .\"O .B O_EXCL
498 .\"O flag of
499 .\"O .BR open (2)).
500 .\"O If the file already exists,
501 .\"O .BR fopen ()
502 .\"O fails, and sets
503 .\"O .I errno
504 .\"O to
505 .\"O .BR EEXIST .
506 ファイルを排他的にオープンする
507 .RB ( open (2)
508
509 .B O_EXCL
510 フラグと同様)。
511 ファイルがすでに存在する場合、
512 .BR fopen ()
513 は失敗し、
514 .I errno
515
516 .B EEXIST
517 がセットされる。
518 .\"O This flag is ignored for
519 .\"O .BR fdopen ().
520 このフラグは
521 .BR fdopen ()
522 では無視される。
523 .\" FIXME document /,ccs= charset/
524 .\"O .SH "SEE ALSO"
525 .SH 関連項目
526 .BR open (2),
527 .BR fclose (3),
528 .BR fileno (3),
529 .BR fmemopen (3),
530 .BR fopencookie (3)