fmemopenのヘルプ・マニュアル
日本語 英語
fmemopen --help
man fmemopen
FMEMOPEN(3) Linux Programmer’s Manual FMEMOPEN(3)
名前
fmemopen, open_memstream, open_wmemstream - メモリをストリームとしてオ
ープンする
書式
#define _GNU_SOURCE
#include
FILE *fmemopen(void *buf, size_t size, const char *mode);
FILE *open_memstream(char **ptr, size_t *sizeloc);
#define _GNU_SOURCE
#include
FILE *open_wmemstream(wchar_t **ptr, size_t *sizeloc);
説明
fmemopen() 関数は、ストリームをオープンし、そのストリームに mode で指定
されたアクセス許可を設定する。そのストリームを通じて、 buf で指定された
文字列やメモリバッファへの読み書きができる。このバッファは少な く と も
size バイトの長さでなければならない。
引 き数 mode は fopen(3) の場合と同じである。 mode で追記モード (append
mode) が指定された場合、ファイル位置の初期値はバッファ中の最初 の NULL
バ イト ('\0') の位置に設定される。それ以外の場合は、ファイル位置の初期
値はバッファの先頭になる。 glibc 2.9 以降では、文字 ’b’ を mode の二 番
目 の文字として指定することができる。この文字は「バイナリ」モードを指定
するものである。このモードでは、書き込み時に文字列終端のヌルバイトが 黙
って追加されることはない。また、 fseek(3) SEEK_END は、文字列の長さから
の相対値ではなく、バッファの末尾 (size で指定した値) からの相対値となる
。
書き込み用にオープンされたストリームをフラッシュ (fflush(3)) やクローズ
(fclose(3)) した時に、 (バッファに空きがあれば) NULL バイトがバッファの
末 尾に書き込まれる。このようにするためには、呼び出し元はバッファに 1バ
イト余裕を作る (size にこの 1バイトを含めた値を指定する) 必要がある。
バッファに size バイトよりたくさん書き込もうとした場合には、エラーと な
る。 (デフォルトでは、このようなエラーは stdio バッファがフラッシュされ
た時にしか見えない。 setbuf(fp, NULL) を使ってバッファリングを無効に す
る 方法は、出力操作を行ったときにエラーを検出するのに役立つ。別の方法と
しては、 setbuffer(fp, buf, size) を使って、呼び出し側が明示的に stdio
ストリームバッファとして buf を指定し、バッファの指定時にバッファのサイ
ズを stdio に教える方法がある。)
読み出し用にオープンされたストリームでは、バッファ内に NULL バ イ ト
('\0') があっても読み出し操作がファイル末尾 (end-of-file) を返すことは
ない。バッファからの読み出しでファイル末尾が返るのは、ファイルポイン タ
が バッファの先頭から size バイトを越えて先に進もうとした場合だけである
。
buf に NULL が指定された場合、 fmemopen() は動的に size バイトの長さ の
バ ッファを確保する。この方法は、一時バッファにデータの書き込みを行って
から、その内容を再度読み出すようなアプリケーションで有用である。この バ
ッ ファはストリームがクローズされるときに自動的に解放される。呼び出し元
からはこの関数が割り当てた一時バッファへのポインタ値を知る方法は存在 し
ない点に注意 (下記の open_memstream() も参照)。
open_memstream() 関数は、バッファへの書き込み用にストリームをオープンす
る。バッファは (malloc(3) を使って) 動的に割り当てられ、必要に応じて 自
動 的に伸長する。ストリームをクローズした後で、読み出し元はこのバッファ
を free(3) すべきである。
このストリームがクローズ (fclose(3)) されたりフラッシュ (fflush(3)) さ
れ た時に、 ptr と sizeloc の値はそれぞれバッファへのポインタとそのサイ
ズに更新される。これらの値は、呼び出し元がそのストリームに新たな書き 込
み を行わない場合に限り有効である。ストリームに書き込みを行った際には、
これらの変数を参照する前にストリームを再度フラッシュしなければならな い
。
バッファ末尾の NULL バイトは保持される。この NULL バイトは sizeloc に格
納されるサイズには「含まれない」。
ストリームのファイル位置は fseek(3) or fseeko(3) で変更できる。すでにデ
ー タが書き込まれた領域の末尾より先にファイル位置を動かすと、その間の領
域は 0 で埋められる。
open_wmemstream() は open_memstream() と同様だが、バイトではなくワイ ド
文字に対して操作を行う点が異なる。
返り値
成 功 し て 終了した場合には、 fmemopen(), open_memstream(), open_wmem-
stream() は FILE ポインタを返す。失敗した場合は、 NULL を返し 、 errno
にエラーを示す値をセットする。
バージョン
fmemopen() と open_memstream() は glibc 1.0.x ですでに利用可能であった
。 open_wmemstream() は glibc 2.4 以降で利用可能である。
準拠
POSIX.1-2008. これらの関数は POSIX.1-2001 では規定れていないが、 Linux
以外のシステムで広く利用可能である。これらの関数は GNU 拡張である。
例
こ の プ ロ グ ラ ム は fmemopen() を使って出力バッファをオープンし、
open_memstream() を使って動的にサイズが変化する出力バッファをオープンし
て いる。 (プログラムの第一コマンドライン引き数から取った) 入力文字列を
スキャンして整数を読み込み、これらの整数の二乗を出力バッファに書き出 す
。このプログラムの実行例は以下のようになる。
$ ./a.out '1 23 43'
size=11; ptr=1 529 1849
プログラムのソース
#define _GNU_SOURCE
#include
#include
#include
#include
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
FILE *out, *in;
int v, s;
size_t size;
char *ptr;
assert(argc == 2);
in = fmemopen(argv[1], strlen(argv[1]), "r");
if (in == NULL)
handle_error("fmemopen");
out = open_memstream(&ptr, &size);
if (out == NULL)
handle_error("fmemopen");
for (;;) {
s = fscanf(in, "%d", &v);
if (s <= 0)
break;
s = fprintf(out, "%d ", v * v);
if (s == -1)
handle_error("fprintf");
}
fclose(in);
fclose(out);
printf("size=%ld; ptr=%s\n", (long) size, ptr);
free(ptr);
exit(EXIT_SUCCESS);
}
バグ
バ ージョン 2.7 より前の glibc では、 open_memstream() で作成されたスト
リームの末尾より先にファイル位置を動かしても、バッファが伸長さ れ ず 、
fseek() が失敗し -1 が返る。
関連項目
fopen(3), fopencookie(3), feature_test_macros(7)
GNU 2009-02-22 FMEMOPEN(3)
FMEMOPEN(3) Linux Programmer’s Manual FMEMOPEN(3)
NAME
fmemopen, open_memstream, open_wmemstream - open memory as stream
SYNOPSIS
#define _GNU_SOURCE
#include
FILE *fmemopen(void *buf, size_t size, const char *mode);
FILE *open_memstream(char **ptr, size_t *sizeloc);
#define _GNU_SOURCE
#include
FILE *open_wmemstream(wchar_t **ptr, size_t *sizeloc);
DESCRIPTION
The fmemopen() function opens a stream that permits the access speci-
fied by mode. The stream allows I/O to be performed on the string or
memory buffer pointed to by buf. This buffer must be at least size
bytes long.
The argument mode is the same as for fopen(3). If mode specifies an
append mode, then the initial file position is set to the location of
the first null byte ('\0') in the buffer; otherwise the initial file
position is set to the start of the buffer. Since glibc 2.9, the let-
ter ’b’ may be specified as the second character in mode. This pro-
vides "binary" mode: writes don’t implicitly add a terminating null
byte, and fseek(3) SEEK_END is relative to the end of the buffer (i.e.,
the value specified by the size argument), rather than the current
string length.
When a stream that has been opened for writing is flushed (fflush(3))
or closed (fclose(3)), a null byte is written at the end of the buffer
if there is space. The caller should ensure that an extra byte is
available in the buffer (and that size counts that byte) to allow for
this.
Attempts to write more than size bytes to the buffer result in an
error. (By default, such errors will only be visible when the stdio
buffer is flushed. Disabling buffering with setbuf(fp, NULL) may be
useful to detect errors at the time of an output operation. Alterna-
tively, the caller can explicitly set buf as the stdio stream buffer,
at the same time informing stdio of the buffer’s size, using set-
buffer(fp, buf, size).)
In a stream opened for reading, null bytes ('\0') in the buffer do not
cause read operations to return an end-of-file indication. A read from
the buffer will only indicate end-of-file when the file pointer
advances size bytes past the start of the buffer.
If buf is specified as NULL, then fmemopen() dynamically allocates a
buffer size bytes long. This is useful for an application that wants
to write data to a temporary buffer and then read it back again. The
buffer is automatically freed when the stream is closed. Note that the
caller has no way to obtain a pointer to the temporary buffer allocated
by this call (but see open_memstream() below).
The open_memstream() function opens a stream for writing to a buffer.
The buffer is dynamically allocated (as with malloc(3)), and automati-
cally grows as required. After closing the stream, the caller should
free(3) this buffer.
When the stream is closed (fclose(3)) or flushed (fflush(3)), the loca-
tions pointed to by ptr and sizeloc are updated to contain, respec-
tively, a pointer to the buffer and the current size of the buffer.
These values remain valid only as long as the caller performs no fur-
ther output on the stream. If further output is performed, then the
stream must again be flushed before trying to access these variables.
A null byte is maintained at the end of the buffer. This byte is not
included in the size value stored at sizeloc.
The stream’s file position can be changed with fseek(3) or fseeko(3).
Moving the file position past the end of the data already written fills
the intervening space with zeros.
The open_wmemstream() is similar to open_memstream(), but operates on
wide characters instead of bytes.
RETURN VALUE
Upon successful completion fmemopen(), open_memstream() and open_wmem-
stream() return a FILE pointer. Otherwise, NULL is returned and errno
is set to indicate the error.
VERSIONS
fmemopen() and open_memstream() were already available in glibc 1.0.x.
open_wmemstream() is available since glibc 2.4.
CONFORMING TO
POSIX.1-2008. These functions are not specified in POSIX.1-2001, and
are not widely available on other systems.
NOTES
There is no file descriptor associated with the file stream returned by
these functions (i.e., fileno(3) will return an error if called on the
returned stream).
BUGS
In glibc before version 2.7, seeking past the end of a stream created
by open_memstream() does not enlarge the buffer; instead the fseek()
call fails, returning -1.
EXAMPLE
The program below uses fmemopen() to open an input buffer, and
open_memstream() to open a dynamically sized output buffer. The pro-
gram scans its input string (taken from the program’s first command-
line argument) reading integers, and writes the squares of these inte-
gers to the output buffer. An example of the output produced by this
program is the following:
$ ./a.out '1 23 43'
size=11; ptr=1 529 1849
Program source
#define _GNU_SOURCE
#include
#include
#include
#include
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
FILE *out, *in;
int v, s;
size_t size;
char *ptr;
assert(argc == 2);
in = fmemopen(argv[1], strlen(argv[1]), "r");
if (in == NULL)
handle_error("fmemopen");
out = open_memstream(&ptr, &size);
if (out == NULL)
handle_error("fmemopen");
for (;;) {
s = fscanf(in, "%d", &v);
if (s <= 0)
break;
s = fprintf(out, "%d ", v * v);
if (s == -1)
handle_error("fprintf");
}
fclose(in);
fclose(out);
printf("size=%ld; ptr=%s\n", (long) size, ptr);
free(ptr);
exit(EXIT_SUCCESS);
}
SEE ALSO
fopen(3), fopencookie(3), feature_test_macros(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2009-04-21 FMEMOPEN(3)