Avatar of chsalvia
chsalvia

asked on 

Flags to create new file with fstream

What are the needed flags to create a new file for input and output using fstream?

I seem to get different results on different computers.  On some computers the following code successfully creates a new file.  On others, it outputs the error "No such file or directory".  Note that the file "test.txt" does not exist - it should be created with the call to the fstream constructor.


#include <iostream>
#include <fstream>
using namespace std;
 
int main()
{
        std::fstream file("test.txt", std::ios::in | std::ios::out);
        if (file.fail()) perror("");
        else cout << "Success!" << endl;
}

Open in new window

C++

Avatar of undefined
Last Comment
Infinity08

8/22/2022 - Mon