I am using fstream to open and close a file. The code works OK, except the the file does not close when I close it the first time. I need to invoke close twice.

Any idea why?

And, what this is saying about my code that I do not realize??

The code:

      fstream cif;
      cif.open(INFO.c_str(), ios::in);
      MYgetline(cif, cifline);// header
      MYgetline(cif, cifline);; // pointer cif
      Vector cifrule(countcommas(cifline)-1);
      int comma=2;
      for (int i=0; i<cifrule.length(); i++) cifrule(i)=comma_read(comma,cifline);
      for (int c=0; c<cifrule.length(); c++)

      if (cif) cout << "cif open\n"; else cout << "cif closed\n";
      if (cif) cout << "cif open\n"; else cout << "cif closed\n";

The output:

cif open
cif closed

Recommended Answers

All 4 Replies

Thanks. I didn't know about is_open. I'll try it.

Still, given the example in http://www.cplusplus.com/reference/ios/ios/operator_bool/ I do not see the difference.

Would is_open generate an error, as opposed to a "false" result, if there was an error flag set when trying to open a file? If not, then under what circumstances would you NEED to code

if (filename.is_open()) do something

as opposed to

if (filename) do something

I suppose you might tell me that I need to use is_open when I want to test for a file being open as in the example of my original question. But, when I did it there is was a nice, but not necessary, check to see if I had closed the files. When would I NEED to use is_open under more realisitic circumstances?

The since you are using operator bool it will only be false when badbit and or failbit is set. The first time you close the file and evaluate operator bool none of those are set so the operator returns true. Calling close() on the stream a second time will fail since it is already closed and since the function fails the failbit is set. The next time you evaluate the stream using operator bool it returns false.

Still, given the example in http://www.cplusplus.com/reference/ios/ios/operator_bool/ I do not see the difference.

Look harder.

In the example code at http://www.cplusplus.com/reference/ios/ios/operator_bool/ they attempted to open the file, and then tested for something having gone wrong. They're not testing that it opened. They're testing for any bad bits set. They're testing that something went wrong.

So in your code, you were trying to close it, and then you were testing for something having gone wrong. Were you expecting something to have gone wrong? No. If it closed successfully, is that something having gone wrong? No. So the test will come back and say "Nothing went wrong".

Do you see the difference now?

You could test for the file being open in any situation where there is a chance that an attempt to open or close it went wrong, and you care about whether or not it went wrong. They go wrong for various reasons. Wrong filename, wrong directory, non-existent file, network filesystem problems, you already closed it before and now you're trying to close it again, you already opened it before and now you're trying to open it again. When SHOULD you test? Hey, YOU'RE the programmer. Do when YOU think it's necessary, given your particular conditions and circumstances. Programming is thinking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.