简体   繁体   English

ifstream无法在qtcreator中打开文件

[英]Ifstream fail to open a file in qtcreator

Please look at the below program and output. 请看下面的程序和输出。

main.cpp main.cpp

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

openfile function: openfile函数:

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. m_File是网格类的私有ifstream对象。

I checked the input file name and verified with the string argument in the called function, and checked the permissions of 'input.in' file. 我检查了输入文件名,并在调用的函数中使用字符串参数进行了验证,并检查了“ input.in”文件的权限。 I couldn't find a mistake there. 我在那里找不到错误。 Then why Qt creator gives me the following output: 那么为什么Qt Creator给我以下输出:

couldn't open input.in

My .pro file is: 我的.pro文件是:

 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 \
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). 默认情况下,Qt Creator将应用程序进程的工作目录设置为创建应用程序的目录(或在Windows上可以是一个以上级别)。

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 另请参阅Qt Creator文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM