creating a file

I want to open a file for a fstream-object and i want the file to be created if it doesn't exist yet.
This one works:
1
2
std::fstream mystream;
mystream.open("test.txt", std::ios::out);


This one doesn't:
1
2
std::fstream mystream;
mystream.open("test.txt", std::ios::in|std::ios::out);


Is there a good solution for this problem?
1
2
3
4
std::fstream mystream;
mystream.open("test.txt", std::ios::out);
mystream.close();
mystream.open("test.txt", std::ios::in|std::ios::out);


Open/create as an output file, then close. That way it will definately exist when you open it for input and output, although maybe not efficient?
I already thought about that. The second idea is keeping instream and outstream separate, wich would also have the advantage of separate input and output pointers.

But maybe someone comes up with some other good idea. I am wondering, if there is even an ios-flag to control that.
I believe that std::ios::app will add onto the file if it exists or create a new file if it doesn't.

EDIT: ios::app calls ios::out first so it should work as creating a file and/or appending to a file
Last edited on
Topic archived. No new replies allowed.