user294664 user294664 - 6 days ago 5
C++ Question

Ifstream fail to open a file in qtcreator

Please look at the below program and output.

main.cpp

#include<iostream>
#include "mesh.h"
using namespace std;
int main()
{
mesh trail;
trail.openfile("input.in");
}


openfile function:

void mesh::openfile(std::string szFile)
{
m_File.open(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 'input.in' file. I couldn't find a mistake there. Then why Qt creator gives me the following output:

couldn't open input.in


My .pro file is:

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

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


HEADERS += \
node.h \
element.h \
mesh.h

DISTFILES += \
input.in \

Answer
trail.openfile("input.in");

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

Comments