【问题标题】:I can't read a file with ifstream in a build, but can read it in an IDE我无法在构建中使用 ifstream 读取文件,但可以在 IDE 中读取它
【发布时间】: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 那么您正在运行的文件在哪里?我在问题的文件夹布局中没有看到它。

标签: c++ file-io ifstream


【解决方案1】:

我能看到的唯一方法是从可执行文件的目录中获取整个文件路径,剪切文件,然后用我想要的文件路径替换它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-04
    • 2010-12-09
    • 1970-01-01
    • 2011-12-13
    • 2019-06-22
    相关资源
    最近更新 更多