0

I've got this code that uses an fstream to read and write to a file. The fstream object is held as a member of an object, and is initialized in the constructor like so:

idmap.open(path, std::fstream::in | std::fstream::out | std::fstream::app);

The file gets properly created if it doesn't already exists. Then it gets written to like so:

idmap.seekp(0, std::fstream::end);
idmap << str.size() << ':' << str << '\n';
idmap.flush();
idmap.sync();

It's supposed to be read like this but I don't know if it works because the file has always been empty:

idmap.seekg(0);
while (!idmap.eof()) {
    idmap.getline(line, 1024);

    idtype id = getIDMapEntry(std::string(line));
    if (identifier.compare(nfile.getIdentifier()) == 0) {
        return nfile;
    }
}

Then it's closed when the program exits:

idmap.close();

It's probably something else in the program but I figure I'll ask here in case I did something stupid, and dig through everything else in parallel.

6
  • 1
    It should be easy enough for you to create a small stand alone program that tests just what you've posted here. That will help pinpoint where your problem is. Commented Aug 17, 2012 at 19:04
  • Did you try flushing the stream? Commented Aug 17, 2012 at 19:10
  • Yes, just forgot to include that in the example code. Commented Aug 17, 2012 at 19:19
  • 1
    Aside: I don't recommend using .eof() as a loop condition. It almost always produces buggy code (as it has in this case). Instead, prefer while(idmap.getline(line, 1024)) { ... }. Or, even better, using std::getline, thus: std::string line; while(std::getline(idmap, line)) { ... }. Commented Aug 17, 2012 at 20:36
  • What makes you believe that the file is empty? Is it that compare function does or doesn't trip? If so, you are suffering from the .eof() bug. In your case, it causes an extra blank line to appear in your input. That blank line presumably fails your getIDMapEntry(). Commented Aug 17, 2012 at 20:47

1 Answer 1

1

Works for me.

This program, except for the .eof() bug, works precisely as expected:

#include <fstream>
#include <iostream>

int main() {
  std::fstream idmap;
  const char path[] = "/tmp/foo.txt";
  idmap.open(path, std::fstream::in | std::fstream::out | std::fstream::app);

  std::string str("She's no fun, she fell right over.");
  idmap.seekp(0, std::fstream::end);
  idmap << str.size() << ':' << str << '\n';
  idmap.flush();
  idmap.sync();

  idmap.seekg(0);
#if 1
  // As the user presented, with .eof() bug
  char line[1024];
  while (!idmap.eof())
  {
    idmap.getline(line, 1024);

    std::cout << line << "\n";
  }
#else
  // With fix for presumably unrelated .eof() bug
  std::string line;
  while(std::getline(idmap, line)) {
    std::cout << line << "\n";
  }
#endif

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the thorough description. I'll mark this as the answer although it doesn't solve my problem, since it does prove my question wasn't thorough enough. Although there really isn't anything else in the entire application that I think could interfere with file handling, and a separate file did write correctly using ofstream in the same directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.