Unable to write more than 16K bytes files !!! Why ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Im 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 !!!
The code is simple, fopen ... fread/fwrite ... fflush ... fclose !
I have try with C++. But now I got a problem during the "read"
in BinaryDefaultRead it return -1 !!! But really dont know why ! I only write 4 bytes at a time !!!
It is under Win7 64 bits with MSVC 2008 compiler.
What is strange is that I continue to write and only 16kbytes are written ! (And the content is correct)
If someone has an idea ?
<div style="color:Black;background-color:White; <pre>
#include <fstream>
#include <iostream>

<span style="color:Blue; using <span style="color:Blue; namespace std;

size_t BinaryDefaultRead(ifstream& stream, <span style="color:Blue; void* buffer, <span style="color:Blue; unsigned <span style="color:Blue; int bufferSize)
{
stream.read((<span style="color:Blue; char*)buffer, bufferSize);
<span style="color:Blue; int s = stream.gcount();
<span style="color:Blue; if (!stream)
<span style="color:Blue; return -1;

<span style="color:Blue; return bufferSize;
}

size_t BinaryDefaultWrite(ofstream& stream, <span style="color:Blue; const <span style="color:Blue; void* buffer, <span style="color:Blue; unsigned <span style="color:Blue; int bufferSize)
{
stream.write((<span style="color:Blue; char*)buffer, bufferSize);
<span style="color:Blue; if (!stream)
<span style="color:Blue; return -1;

<span style="color:Blue; return bufferSize;
}

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

<span style="color:Blue; if (result < 0)
<span style="color:Blue; return result;

*<span style="color:Blue; value = ((<span style="color:Blue; unsigned <span style="color:Blue; int) buf[0]) |
(((<span style="color:Blue; unsigned <span style="color:Blue; int) buf[1]) << 8) |
(((<span style="color:Blue; unsigned <span style="color:Blue; int) buf[2]) << 16) |
(((<span style="color:Blue; unsigned <span style="color:Blue; int) buf[3]) << 24);

<span style="color:Blue; return result;
}

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

<span style="color:Blue; return BinaryDefaultWrite(stream, (<span style="color:Blue; void*)buf, 4);
}

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

<span style="color:Blue; if (result < 0)
<span style="color:Blue; return result;

*<span style="color:Blue; value = u.f;

<span style="color:Blue; return result;
}

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

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

<span style="color:Blue; return result;
}

size_t BinaryWriteUINTArray(ofstream& stream, <span style="color:Blue; unsigned <span style="color:Blue; int* buffer, <span style="color:Blue; unsigned <span style="color:Blue; int count)
{
size_t result;
<span style="color:Blue; for(<span style="color:Blue; unsigned <span style="color:Blue; int i = 0; i < count; i++)
{
result = BinaryWriteUINT(stream, buffer);
<span style="color:Blue; if (result < 0)
<span style="color:Blue; return result;
}

<span style="color:Blue; return result;
}

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

<span style="color:Blue; return result;
}

size_t BinaryWriteFLOATArray(ofstream& stream, <span style="color:Blue; float* buffer, <span style="color:Blue; unsigned <span style="color:Blue; int count)
{
size_t result;
<span style="color:Blue; for(<span style="color:Blue; unsigned <span style="color:Blue; int i = 0; i < count; i++)
{
result = BinaryWriteFLOAT(stream, buffer);
<span style="color:Blue; if (result < 0)
<span style="color:Blue; return result;
}

<span style="color:Blue; return result;
}
[/code] <hr class="sig PL01

View the full article
 
Back
Top