1 .\" Copyright 2005 walter harms (walter.harms@informatik.uni-oldenburg.de),
2 .\" and Copyright 2005 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Distributed under the GPL.
4 .\" 2008-12-04, Petr Baudis <pasky@suse.cz>: Document open_wmemstream()
6 .\" Japanese Version Copyright (c) 2005, 2006 Akihiro MOTOKI
7 .\" all rights reserved.
8 .\" Translated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
9 .\" Updated 2006-01-20, Akihiro MOTOKI
10 .\" Updated 2006-07-20, Akihiro MOTOKI
11 .\" Updated 2008-11-08, Akihiro MOTOKI, LDP v3.13
12 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24
14 .TH FMEMOPEN 3 2010-09-15 "GNU" "Linux Programmer's Manual"
17 .\"O fmemopen, open_memstream, open_wmemstream \- open memory as stream
18 fmemopen, open_memstream, open_wmemstream \- メモリをストリームとしてオープンする
24 .BI "FILE *fmemopen(void *"buf ", size_t "size ", const char *" mode ");"
26 .BI "FILE *open_memstream(char **" ptr ", size_t *" sizeloc );
30 .BI "FILE *open_wmemstream(wchar_t **" ptr ", size_t *" sizeloc );
35 .\"O Feature Test Macro Requirements for glibc (see
36 .\"O .BR feature_test_macros (7)):
38 .RB ( feature_test_macros (7)
43 .BR open_memstream (),
44 .BR open_wmemstream ():
49 .\"O Since glibc 2.10:
51 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
53 .\"O Before glibc 2.10:
63 .\"O function opens a stream that permits the access specified by
65 .\"O The stream allows I/O to be performed on the string or memory buffer
68 .\"O This buffer must be at least
72 関数は、ストリームをオープンし、そのストリームに
77 で指定された文字列やメモリバッファへの読み書きができる。
84 .\"O is the same as for
88 .\"O specifies an append mode, then the initial file position is set to
89 .\"O the location of the first null byte (\(aq\\0\(aq) in the buffer;
90 .\"O otherwise the initial file position is set to the start of the buffer.
97 で追記モード (append mode) が指定された場合、ファイル位置の初期値は
98 バッファ中の最初の NULL バイト (\(aq\\0\(aq) の位置に設定される。
99 それ以外の場合は、ファイル位置の初期値はバッファの先頭になる。
100 .\"O Since glibc 2.9,
101 .\"O the letter 'b' may be specified as the second character in
103 .\"O This provides "binary" mode:
104 .\"O writes don't implicitly add a terminating null byte, and
107 .\"O is relative to the end of the buffer (i.e., the value specified by the
109 .\"O argument), rather than the current string length.
110 glibc 2.9 以降では、文字 'b' を
112 の二番目の文字として指定することができる。
113 この文字は「バイナリ」モードを指定するものである。
114 このモードでは、書き込み時に文字列終端のヌルバイトが黙って追加
118 は、文字列の長さからの相対値ではなく、バッファの末尾
122 .\"O When a stream that has been opened for writing is flushed
123 .\"O .RB ( fflush (3))
125 .\"O .RB ( fclose (3)),
126 .\"O a null byte is written at the end of the buffer if there is space.
127 .\"O The caller should ensure that an extra byte is available in the
131 .\"O counts that byte)
132 .\"O to allow for this.
133 書き込み用にオープンされたストリームをフラッシュ
138 (バッファに空きがあれば) NULL バイトがバッファの末尾に書き込まれる。
142 にこの 1バイトを含めた値を指定する) 必要がある。
144 .\"O Attempts to write more than
146 .\"O bytes to the buffer result in an error.
149 バイトよりたくさん書き込もうとした場合には、エラーとなる。
150 .\"O (By default, such errors will only be visible when the
152 .\"O buffer is flushed.
153 .\"O Disabling buffering with
154 .\"O .I setbuf(fp,\ NULL)
155 .\"O may be useful to detect errors at the time of an output operation.
156 .\"O Alternatively, the caller can explicitly set
158 .\"O as the stdio stream buffer, at the same time informing stdio
159 .\"O of the buffer's size, using
160 .\"O .IR "setbuffer(fp, buf, size)" .)
161 (デフォルトでは、このようなエラーが見えるのは
165 を使ってバッファリングを無効にする方法は、
166 出力操作を行った時点でエラーを検出するのに役立つ。
168 .IR "setbuffer(fp, buf, size)"
172 を指定し、バッファの指定時にバッファのサイズを
174 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=1995
176 .\" http://sources.redhat.com/ml/libc-alpha/2006-04/msg00064.html
178 .\"O In a stream opened for reading,
179 .\"O null bytes (\(aq\\0\(aq) in the buffer do not cause read
180 .\"O operations to return an end-of-file indication.
181 .\"O A read from the buffer will only indicate end-of-file
182 .\"O when the file pointer advances
184 .\"O bytes past the start of the buffer.
185 読み出し用にオープンされたストリームでは、
186 バッファ内に NULL バイト (\(aq\\0\(aq) があっても
187 読み出し操作がファイル末尾 (end-of-file) を返すことはない。
188 バッファからの読み出しでファイル末尾が返るのは、
191 バイトを越えて先に進もうとした場合だけである。
195 .\"O is specified as NULL, then
197 .\"O dynamically allocates a buffer
206 .\"O This is useful for an application that wants to write data to
207 .\"O a temporary buffer and then read it back again.
208 .\"O The buffer is automatically freed when the stream is closed.
209 .\"O Note that the caller has no way to obtain a pointer to the
210 .\"O temporary buffer allocated by this call (but see
211 .\"O .BR open_memstream ()
213 この方法は、一時バッファにデータの書き込みを行ってから、
214 その内容を再度読み出すようなアプリケーションで有用である。
215 このバッファはストリームがクローズされるときに自動的に解放される。
216 呼び出し元からはこの関数が割り当てた一時バッファへのポインタ値を
218 .BR open_memstream ()
222 .\"O .BR open_memstream ()
223 .\"O function opens a stream for writing to a buffer.
225 .\"O is dynamically allocated (as with
226 .\"O .BR malloc (3)),
227 .\"O and automatically grows as required.
228 .\"O After closing the stream, the caller should
231 .BR open_memstream ()
232 関数は、バッファへの書き込み用にストリームをオープンする。
235 を使って) 動的に割り当てられ、必要に応じて自動的に伸長する。
236 ストリームをクローズした後で、呼び出し元はこのバッファを
240 .\"O When the stream is closed
241 .\"O .RB ( fclose (3))
243 .\"O .RB ( fflush (3)),
244 .\"O the locations pointed to by
248 .\"O are updated to contain, respectively, a pointer to the buffer and the
249 .\"O current size of the buffer.
250 .\"O These values remain valid only as long as the caller
251 .\"O performs no further output on the stream.
252 .\"O If further output is performed, then the stream
253 .\"O must again be flushed before trying to access these variables.
263 の値はそれぞれバッファへのポインタとそのサイズに更新される。
264 これらの値は、呼び出し元がそのストリームに新たな書き込みを
266 ストリームに書き込みを行った際には、これらの変数を参照する前に
267 ストリームを再度フラッシュしなければならない。
269 .\"O A null byte is maintained at the end of the buffer.
272 .\"O included in the size value stored at
274 バッファ末尾の NULL バイトは保持される。
279 .\"O The stream's file position can be changed with
283 .\"O Moving the file position past the end
284 .\"O of the data already written fills the intervening space with
291 すでにデータが書き込まれた領域の末尾より先にファイル位置を動かすと、
295 .\"O .BR open_wmemstream ()
297 .\"O .BR open_memstream (),
298 .\"O but operates on wide characters instead of bytes.
299 .BR open_wmemstream ()
301 .BR open_memstream ()
302 と同様だが、バイトではなくワイド文字に対して操作を行う点が異なる。
303 .\"O .SH "RETURN VALUE"
305 .\"O Upon successful completion
306 .\"O .BR fmemopen (),
307 .\"O .BR open_memstream ()
309 .\"O .BR open_wmemstream ()
313 .\"O Otherwise, NULL is returned and
315 .\"O is set to indicate the error.
318 .BR open_memstream (),
319 .BR open_wmemstream ()
330 .\"O .BR open_memstream ()
331 .\"O were already available in glibc 1.0.x.
332 .\"O .BR open_wmemstream ()
333 .\"O is available since glibc 2.4.
336 .BR open_memstream ()
337 は glibc 1.0.x ですでに利用可能であった。
338 .BR open_wmemstream ()
339 は glibc 2.4 以降で利用可能である。
340 .\"O .SH "CONFORMING TO"
343 .\"O These functions are not specified in POSIX.1-2001,
344 .\"O and are not widely available on other systems.
345 これらの関数は POSIX.1-2001 では規定れていないが、
346 Linux 以外のシステムで広く利用可能である。
349 .\"O There is no file descriptor associated with the file stream
350 .\"O returned by these functions
353 .\"O will return an error if called on the returned stream).
354 これらの関数が返すファイルストリームに対応するファイル
355 ディスクリプタはない (つまり、返されたストリームに対して
360 .\"O In glibc before version 2.7, seeking past the end of a stream created by
361 .\"O .BR open_memstream ()
362 .\"O does not enlarge the buffer; instead the
364 .\"O call fails, returning \-1.
365 .\"O .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
366 バージョン 2.7 より前の glibc では、
367 .BR open_memstream ()
368 で作成されたストリームの末尾より先にファイル位置を動かしても、
372 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
375 .\"O The program below uses
377 .\"O to open an input buffer, and
378 .\"O .BR open_memstream ()
379 .\"O to open a dynamically sized output buffer.
380 .\"O The program scans its input string (taken from the program's
381 .\"O first command-line argument) reading integers,
382 .\"O and writes the squares of these integers to the output buffer.
383 .\"O An example of the output produced by this program is the following:
387 .BR open_memstream ()
388 を使って動的にサイズが変化する出力バッファをオープンしている。
389 (プログラムの第一コマンドライン引き数から取った) 入力文字列を
390 スキャンして整数を読み込み、これらの整数の二乗を出力バッファに書き出す。
391 このプログラムの実行例は以下のようになる。
395 .RB "$" " ./a.out \(aq1 23 43\(aq"
396 size=11; ptr=1 529 1849
399 .\"O .SS Program source
408 #define handle_error(msg) \\
409 do { perror(msg); exit(EXIT_FAILURE); } while (0)
412 main(int argc, char *argv[])
420 fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
424 in = fmemopen(argv[1], strlen(argv[1]), "r");
426 handle_error("fmemopen");
428 out = open_memstream(&ptr, &size);
430 handle_error("open_memstream");
433 s = fscanf(in, "%d", &v);
437 s = fprintf(out, "%d ", v * v);
439 handle_error("fprintf");
443 printf("size=%ld; ptr=%s\\n", (long) size, ptr);