-
February 19th, 2009, 07:00 AM
#1
Dev-C++ error?
Hi!
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:
Code:
void moveRecords(fstream & f) {
f.seekg(0,ios::beg);
int x=0,y=0;
f.read((char *)&x,sizeof(int));
if (f) {
f.read((char *)&y,sizeof(int));
while (f) {
f.seekp(-4,ios::cur);
f.write((char *)&x,sizeof(int));
x=y;
f.tellp();
f.read((char *)&y,sizeof(int));
}
}
f.clear();
f.seekp(0,ios::end);
f.write((char *)&x,sizeof(int));
f.clear();
}
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++.
-
February 19th, 2009, 07:49 AM
#2
Re: Dev-C++ error?
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
-
February 19th, 2009, 08:05 AM
#3
Re: Dev-C++ error?
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..
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|