user294664 user294664 - 1 year ago 127
C++ Question

Ifstream fail to open a file in qtcreator

Please look at the below program and output.


#include "mesh.h"
using namespace std;
int main()
mesh trail;

openfile function:

void mesh::openfile(std::string szFile)
if(!m_File) std::cout<< "couldn't open "<<szFile;


m_File is private ifstream object of mesh class.

I checked the input file name and verified with the string argument in the called function, and checked the permissions of '' file. I couldn't find a mistake there. Then why Qt creator gives me the following output:

couldn't open

My .pro file is:

CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp\
node.cpp \
element.cpp \

node.h \
element.h \


Answer Source

This tries to find the file relative to the "current working directory" of the application process. By default Qt Creator sets the working directory of the application process to the directory where the application is created (or on Windows it can be one level above).

Have a look at the working directory in Projects mode > Run settings for your project, and make sure it points to the place where the input file is located. See also the Qt Creator documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download