【发布时间】:2018-11-05 00:09:19
【问题描述】:
当我在 Eclipse 中运行一个应该读取文件并将其内容输出到控制台的程序时,它会毫无问题地读取并输出它,但是当我构建它并在 IDE 之外运行它时它不会不读。
这是读取文件的代码:
Map::Map(std::string file, SE_Graphics *graphics) {
std::ifstream input(file);
if (input.is_open()) {
std::string line;
while (std::getline(input, line)) {
std::cout << line << std::flush;
}
input.close();
} else {
std::cout << "File is missing" << std::flush;
}
}
这就是我调用该方法的方式:
Map map("salsaboy/maps/test.sem", se.getGraphics());
这是文件夹结构:
Lab (the build)
src
salsaboy
|-tiles
|-maps
|-test.sem
每次我在 IDE 之外运行它时,它只会显示 File is missing,即使我尝试写入该文件时 input.is_open() 也不正确。
【问题讨论】:
-
上升了 3 分钟,已经不喜欢没有上下文了。至少告诉我这个问题有什么问题!
-
。 o O ( "
if (input.is_open()) { stuff; stuff; stuff; stuff; stuff; stuff; more_stuff; } else get_the_hell_outa_here();" ...为什么人们会这样做?...if(!input.is_open()) get_the_hell_outa_here(); stuff; stuff; stuff; stuff; stuff; stuff; more_stuff;) -
在 IDE 之外运行时,您在哪里运行它?
-
你确定运行程序时当前工作目录是
/wher/ever/Lab吗? -
@LukePasfield 那么您正在运行的文件在哪里?我在问题的文件夹布局中没有看到它。