2

Consider the following code snippet:

const char * filePath = "C:/blah.mtt";
fstream fs(filePath, ios::in | ios::out | ios::binary);
if (fs.fail())
   std::cout << "Failed to open the file!\n";

the fs.fail() check succeeds always. Does it mean that I can't open a file in both read write mode at the same time?

Creating an empty file first and then running the above code, fs.fail() is false always. What is the rational for such a behavior by the fstream class?

Note: I do have requisite permissions for creating the file. I am trying this on windows 10 using VS2015

Arun
  • 3,138
  • 4
  • 30
  • 41

2 Answers2

4

Does it mean that I can't open a file in both read write mode at the same time?

No, you can do this, but the question is whether you can create a file by doing so.

Generally you'll need to add the trunc flag (ironically one of the options for how to handle an existing file), or remove the in flag (see here).

Yes, this is a bit of a pain, but it comes from how the original POSIX APIs work. Blame them!

Creating an empty file first and then running the above code, fs.fail() is false always. What is the rational for such a behavior by the fstream class?

You can always open a file that exists (well, subject to permissions). That behaviour makes sense.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

the fs.fail() check succeeds always. Does it mean that I can't open a file in both read write mode at the same time?

Refer to @Lightness Races in Orbit's answer for a better explanation.

Creating an empty file first and then running the above code, fs.fail() is false always. What is the rational for such a behavior by the fstream class?

If you look at the constructor definition of fstream you can see that mode defines the way you open it. It has other options like app to append to an existing file. If you open up a file using the following code:

fstream fs(filePath, ios::in | ios::out | ios::binary);

You are saying create a new file if it doesn't exist. Which fails if you pre-created it. You should add the app, ate or truncflag if you want it to open successfully. This depends on what exactly you want to do. However, do note that in between the steps of creating and then opening it doesn't guarantee that the file is still there. You should try to do it in one swoop and let exception handling do its work, since you can never go around the errors anyway.

Neijwiert
  • 985
  • 6
  • 19
  • I think I didn't explain it properly. Without creating an empty file first, the fstream object is always in error state (fs.fail() returns true)... but if I create an empty file first, then fstream object is in good state(fs.fail() returns false)...Isn't this opposite of you are saying... – Arun Feb 28 '18 at 10:38
  • @Arun: No, it's exactly what Neijwiert is saying. Although I don't think this is very clear – Lightness Races in Orbit Feb 28 '18 at 10:50
  • @LightnessRacesinOrbit You are correct, I didn't correctly describe the meaning. Your answer better describes it. Should this question not be marked as duplicate as the post you refer to is pretty much the same? – Neijwiert Feb 28 '18 at 10:51
  • @Neijwiert: Weeeeeeeeell maybe. I found it after I'd started writing and haven't the heart to close it now ^_^ I think we can get away with saying this question can be answered specifically as we need to dispel the misconception as well as give more general advice – Lightness Races in Orbit Feb 28 '18 at 11:03