Isstream & ofstream.

Are you sure the input file contains the correct data?

Have you checked if the input file is even being opened correctly?

Your current code doesn't seem to give any indication that the file operations actually happen.

Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
After ifstream(), add something like:

1
2
if (!fin)
    return (std::cout << "Cannot open input file\n"), 1;


You will then get an error message if the input file can't be opened.
Topic archived. No new replies allowed.