Returning True When ostream Cannot Create a File - C++ File Stream

C++ examples for File Stream:stream

Description

Returning True When ostream Cannot Create a File

Demo Code

#include <iostream> 
#include <fstream> 

using namespace std; 

int main() // w w  w  .j a  v a2  s.  c o  m
{ 
    ofstream outfile("/MyFile.txt"); 
    if (outfile.fail()) { 
        cout << "Couldn't open the file!" << endl; 
        return 0; 
    } 
    outfile << "Hi" << endl; 
    outfile.close(); 

    return 0; 
}

Related Tutorials