fopen - 不能写超过16K? [英] fopen - can't write more than 16K?
问题描述
我目前使用fopen写/读二进制文件。小文件都是罚款。但在某些情况下,当确切的内容是> 16K文件的其余部分是无效的
I'm currently using fopen to write/read binary files. With small files all is fines. But in some cases, when "exactly" the content is > 16K the remainder of the file is invalid !!!
代码很简单,fopen ... fread / fwrite ... fflush ... fclose!
The code is simple, fopen ... fread/fwrite ... fflush ... fclose !
我试着用C ++。但现在我有一个问题,在读
I have try with C++. But now I got a problem during the "read"
在BinaryDefaultRead它返回-1!但真的不知道为什么!
我一次只写4个字节!!!
in BinaryDefaultRead it return -1 !!! But really don't know why ! I only write 4 bytes at a time !!!
它是在Win7 64位与MSVC 2008编译器。
It is under Win7 64 bits with MSVC 2008 compiler.
#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
只用于打开文件流,不能读或写。 fread
和 fwrite
用于执行此操作。
fopen
is only used to open a file stream, not to read or write. fread
and fwrite
are used to do that.
fwrite
和 fread
不要确保写入所有传递的元素:它们返回写入的元素数
fwrite
and fread
don't ensure you to write all the elements you pass them: they return the number of elements written, which can be lesser than the number of elements you passed it.
只需检查返回的值,并保留 fwrite
直到你写出所有的元素或直到流有错误:使用 ferror
来检查错误。
Just check the returned value, and keep fwrite
-ing until you write out all of your elements or until there's an error with the stream: use ferror
to check for an error.
从 fwrite
手动:
fread()和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()不区分文件结束和错误,调用者必须使用feof(3)和ferror(3)来确定发生了什么。
fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
这篇关于fopen - 不能写超过16K?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!