0

I'm working with some (legacy) code that opens a file (binary) and reads a bunch of integers, floats, doubles and characters from it. Some relevant code:

std::ifstream infoFile(path.c_str(), std::ios_base::binary);

while (infoFile.good()){
  infoFile.peek();
  if (infoFile.eof())
    break;

  // Read all kinds of things
}

The above is being called quite a few times (2*86 times in my tests) on the same file, possibly parallel. This is a small piece of code in a large codebase, it's very difficult to extract a standalone piece of code.

After an unpredictable number of file reads, the position of the stream changes unpredictably. I've printed tellg before and after the call to peek in the above code and on these rare occasions it changed! I tried mutex-protecting access to this file, made no difference. I'm completely stumped as to how this would be possible, so if anyone has an idea of how this is actually possible that would be a great help.

Adversus
  • 2,166
  • 20
  • 23
  • I don't agree with being marked as duplicate. My problem was related to but not caused by .good/peek/eof, see my answer. The code that is here also simply works, bad style or not. See my answer for the actual solution, which I posted because it's simply hell to debug and it might help someone in the same predicament. – Adversus Oct 16 '15 at 16:01

1 Answers1

0

I accidentally found it, in a completely unrelated part of the code I saw:

_flushall();

removing these calls (no idea why they were there anyway) solved the problem.

Adversus
  • 2,166
  • 20
  • 23