gpt4 book ai didi

c - 内存中的文件*(无磁盘访问)

转载 作者:行者123 更新时间:2023-11-30 16:47:13 25 4
gpt4 key购买 nike

我们有接受 FILE* (CImg) 的库。出于性能原因,我们希望处理内存中已有的数据而不访问磁盘。目标平台是windows,不幸的是它不支持fmemopen(和funopen)

char* buf = new char[sz];

FILE *fp = fopen("C:\\test.dat", "wb");
int r = setvbuf(fp, buf, _IOFBF, sz);

r = fwrite(src_buf, 1, sz, fp); // Here r contains right size


fseek(fp, 0, SEEK_END);
size_t sz2 = ftell(fp); // sz2 contains right size as well
rewind(fp);

// Test (something like this actually is somewhere deep inside library)
char* read_buf = new char[sz];
r = fread(read_buf, 1, sz, fp); // Zero!

最终的 fread() 无法读取任何内容...有什么建议吗?

最佳答案

可能是因为您使用“wb”(只写)而不是“wb+”(读+写)打开

fopen() function

为了进一步提供帮助,您可以 #include errno.h 并打印出错误代码和字符串。

printf( "errno: %d, error:'%s'\n", errno, strerror( errno ) );

关于c - 内存中的文件*(无磁盘访问),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43403285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com