编码
分类: C/C++
2014-08-03 11:40:31
#define _GNU_SOURCE
#include
FILE *fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs);
struct cookie_io_functions_t {
cookie_read_function_t *read;
cookie_write_function_t *write;
cookie_seek_function_t *seek;
cookie_close_function_t *close;
};
#typedef (*cookie_read_function_t)(char *buf, size_t size);
使用时,cookie由实现者使用,可以为0, io_funcs指定定制函数,具体为4个:read, write,seek,close,
分别对应fread,fwrite,fseed,fclose.
使用例子,fprintf等函数均打印“test”
#define _GNU_SOURCE
#include
#include
ssize_t
memfile_write(void *c, const char *buf, size_t size)
{
fwrite("test\n", 1, 5, stdout);
return size;
}
ssize_t
memfile_read(void *c, char *buf, size_t size)
{
return 0;
}
int
memfile_seek(void *c, off64_t *offset, int whence)
{
return 0;
}
int
memfile_close(void *c)
{
return 0;
}
int
main(int argc, char *argv[])
{
cookie_io_functions_t memfile_func = {
.read = memfile_read,
.write = memfile_write,
.seek = memfile_seek,
.close = memfile_close
};
FILE *fp = fopencookie(NULL, "w+", memfile_func);
fprintf(fp, "asdsf\n");
return 0;
}