0
#include <fstream>
#include <iostream>
#include <cassert>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;
    inFile.open("file.txt");
    assert(!inFile.fail());
    cout << "everythings fine" << endl;
    cin.ignore();
    cin.get();
    return 0;
}

This is my current code, file.txt doesnt exist. The program simply crashes. I was hoping for an error message explaining what exactly when wrong(even though i know that the file doesnt exist.)

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

assert() only shows something when the program is compiled for debug. If compiled for release assert() does nothing. In your example it is better to test for inFile.is_open() , which works in both debug and release mode compiles.

int main()
{
    ifstream inFile;
    ofstream outFile;
    inFile.open("file.txt");
    if( !inFile.is_open() )
    {
          cout << "File not opened\n";
          return 1;
    }
    cout << "everythings fine" << endl;
    cin.ignore();
    cin.get();
    return 0;
}

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.