I noticed a very strange behaviour of Dev-C++ compiler. I have a function which transforms a binary file (containing positive and negative integers as records) by moving all the integers one position (4 bytes) to the left (so the first record in the file is left `empty`). I have written a function like this:
It works fine! Now, if I delete or comment the line `f.tellp();`, nothing works fine anymore - just some first integers are moved and after that a while loop is finished (so I guess some error occurs). And the funny part is that it happens only in Dev-C++ compiler - if compiled with Borland, I don't have to write this useless line of `tellp`.. Why is that and how to get rid of it? Because in the situation, I must use the Dev-C++.
Is it a large file?
Can you read it all into a container and then write it out again?
Code:
void moveRecords(fstream & f)
{
std::deque<int> value_list;
int value;
// Read it all in.
while (f.read((char *)&value, sizeof(int)))
{
value_list.push_back(value);
}
// Remove the first.
value_list.pop_front();
// Write them back.
std::copy(value_list.begin(), value_list.end(), std::ostream_iterator<int>(f, ""));
}
(Untested)
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
No, it is not a large file - I tested the function on file having only 10 records in it.. But the thing is I have to demonstrate the operations within the file itself and I am not allowed to put all the contents of the file in the memory and then to forget about the file..
Bookmarks