#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.)

debug, GetLastError(), etc

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.