gpt4 book ai didi

c++ - fopen - 不能写超过 16K?

转载 作者:太空宇宙 更新时间:2023-11-04 15:53:02 24 4
gpt4 key购买 nike

我目前正在使用 fopen 来写入/读取二进制文件。对于小文件,一切都很好。但在某些情况下,当“恰好”内容大于 16K 时,文件的其余部分无效!!!

代码很简单,fopen ... fread/fwrite ... fflush ... fclose !

我尝试过使用 C++。但是现在我在“阅读”期间遇到了问题

在 BinaryDefaultRead 中返回 -1 !!!但是真的不知道为什么!我一次只写 4 个字节!!!

在 Win7 64 位下使用 MSVC 2008 编译器。

#include <fstream>

using namespace std;

size_t BinaryDefaultRead(ifstream& stream, void* buffer, unsigned int bufferSize)
{
//return fread(buffer, 1, (size_t) bufferSize, file);
stream.read((char*)buffer, bufferSize);
if (!stream)
return -1;

return bufferSize;
}

size_t BinaryDefaultWrite(ofstream& stream, const void* buffer, unsigned int bufferSize)
{
//return fwrite(buffer, 1, (size_t) bufferSize, file);
stream.write((char*)buffer, bufferSize);
if (!stream)
return -1;

return bufferSize;
}

// Read an unsigned integer from a stream in a machine endian independent manner (for portability).
size_t BinaryReadUINT(ifstream& stream, unsigned int* value)
{
unsigned char buf[4];
size_t result = BinaryDefaultRead(stream, (void *)buf, 4);

if (result < 0)
return result;

*value = ((unsigned int) buf[0]) |
(((unsigned int) buf[1]) << 8) |
(((unsigned int) buf[2]) << 16) |
(((unsigned int) buf[3]) << 24);

return result;
}

// Write an unsigned integer to a stream in a machine endian independent manner (for portability).
size_t BinaryWriteUINT(ofstream& stream, unsigned int aValue)
{
unsigned char buf[4];
buf[0] = aValue & 0x000000ff;
buf[1] = (aValue >> 8) & 0x000000ff;
buf[2] = (aValue >> 16) & 0x000000ff;
buf[3] = (aValue >> 24) & 0x000000ff;

return BinaryDefaultWrite(stream, (void*)buf, 4);
}

// Read a floating point value from a stream in a machine endian independent manner (for portability).
size_t BinaryReadFLOAT(ifstream& stream, float* value)
{
union {
float f;
unsigned int i;
} u;
size_t result = BinaryReadUINT(stream, &u.i);

if (result < 0)
return result;

*value = u.f;

return result;
}

// Write a floating point value to a stream in a machine endian independent manner (for portability).
size_t BinaryWriteFLOAT(ofstream& stream, float aValue)
{
union {
float f;
unsigned int i;
} u;
u.f = aValue;
return BinaryWriteUINT(stream, u.i);
}

size_t BinaryReadUINTArray(ifstream& stream, unsigned int* buffer, unsigned int count)
{
size_t result;
for(unsigned int i = 0; i < count; i++)
{
result = BinaryReadUINT(stream, buffer + i);
if (result < 0)
return result;
}

return result;
}

size_t BinaryWriteUINTArray(ofstream& stream, unsigned int* buffer, unsigned int count)
{
size_t result;
for(unsigned int i = 0; i < count; i++)
{
result = BinaryWriteUINT(stream, buffer[i]);
if (result < 0)
return result;
}

return result;
}

size_t BinaryReadFLOATArray(ifstream& stream, float* buffer, unsigned int count)
{
size_t result;
for(unsigned int i = 0; i < count; i++)
{
result = BinaryReadFLOAT(stream, buffer + i);
if (result < 0)
return result;
}

return result;
}

size_t BinaryWriteFLOATArray(ofstream& stream, float* buffer, unsigned int count)
{
size_t result;
for(unsigned int i = 0; i < count; i++)
{
result = BinaryWriteFLOAT(stream, buffer[i]);
if (result < 0)
return result;
}

return result;
}

最佳答案

fopen 仅用于打开文件流,不用于读取或写入。 freadfwrite 用于执行此操作。

fwritefread 不能确保您写入传递给它们的所有元素:它们返回写入的元素数量,这可能小于你传递给它的元素。

只需检查返回值,并保持 fwrite-ing 直到您写出所有元素或直到流出现错误:使用 ferror 来检查一个错误。


来自 fwrite 手册:

fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.

关于c++ - fopen - 不能写超过 16K?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4714679/

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