L
Lars Uffmann
Hi everyone!
It's probably my fault, thus the developer error in the subject, but I'm
having some troubles with ofstream. I am trying to randomly access an
output file - I want to write an index for a larger datafile in here,
with the index file holding - in the correct order - file pointers &
length information for blocks in a big data file. Later on I'll rewrite
the big file from the information in the index file and the data in the
data file.
The rewriting works fine, but when I use the following code to enlarge
the index file (padding with 0), it will either ignore the data written
to the index file in a previous function call (when opened without
ios_base::app), or ignore my attempts to write somewhere in the file
using seekp() - this happens when I open the file with ios_base::app.
(code below)
If you can point me to my error (I have tried basically all combinations
of open modes that seemed to make sense) - or tell me that ofstream is
simply not suited for this (it should be, I think) - thanks a lot!
Best Regards,
Lars
I'll try to break the code down - let's assume buf is defined big enough:
/**/
struct myStruct {
streampos pos;
unsigned int length;
};
#define BUFSIZE 10000
void writeToIndexFile (const char *filename, int datasetNo, myStruct *s)
{
ofstream indexFile;
int filesize, missingBytes;
char buf[BUFSIZE];
indexFile.open (filename, ios_base::binary);
indexFile.seekp (0, ios_base::end); // move to eof position
filesize = indexFile.tellp(); // get putpointer @ eof position
// determine how big the file SHOULD be in order to be able to
// write s to the fixed position (datasetNo-1)*sizeof(myStruct)
missingBytes = (datasetNo - 1)*sizeof (myStruct) - filesize;
if (missingBytes > 0) { // if file not big enough
memset (buf, 0, sizeof(buf)); // make sure only 0 gets appended
indexFile.write (buf, missingBytes); // append just enough bytes
}
indexFile.seekp ((datasetNo-1)*sizeof(myStruct), ios_base::beg);
indexFile.write ((char *) s, sizeof (myStruct));
indexFile.close();
}
/**/
It's probably my fault, thus the developer error in the subject, but I'm
having some troubles with ofstream. I am trying to randomly access an
output file - I want to write an index for a larger datafile in here,
with the index file holding - in the correct order - file pointers &
length information for blocks in a big data file. Later on I'll rewrite
the big file from the information in the index file and the data in the
data file.
The rewriting works fine, but when I use the following code to enlarge
the index file (padding with 0), it will either ignore the data written
to the index file in a previous function call (when opened without
ios_base::app), or ignore my attempts to write somewhere in the file
using seekp() - this happens when I open the file with ios_base::app.
(code below)
If you can point me to my error (I have tried basically all combinations
of open modes that seemed to make sense) - or tell me that ofstream is
simply not suited for this (it should be, I think) - thanks a lot!
Best Regards,
Lars
I'll try to break the code down - let's assume buf is defined big enough:
/**/
struct myStruct {
streampos pos;
unsigned int length;
};
#define BUFSIZE 10000
void writeToIndexFile (const char *filename, int datasetNo, myStruct *s)
{
ofstream indexFile;
int filesize, missingBytes;
char buf[BUFSIZE];
indexFile.open (filename, ios_base::binary);
indexFile.seekp (0, ios_base::end); // move to eof position
filesize = indexFile.tellp(); // get putpointer @ eof position
// determine how big the file SHOULD be in order to be able to
// write s to the fixed position (datasetNo-1)*sizeof(myStruct)
missingBytes = (datasetNo - 1)*sizeof (myStruct) - filesize;
if (missingBytes > 0) { // if file not big enough
memset (buf, 0, sizeof(buf)); // make sure only 0 gets appended
indexFile.write (buf, missingBytes); // append just enough bytes
}
indexFile.seekp ((datasetNo-1)*sizeof(myStruct), ios_base::beg);
indexFile.write ((char *) s, sizeof (myStruct));
indexFile.close();
}
/**/