无法从文件中读取

Unable to read from file

在按照 vulkan-tutorial 进行操作时,我遇到了一个问题。我就是用他的方法打不开文件

我有一个名为shader.frag.spv的文件,它是编译为spir-v代码的片段着色器。它在我的源文件夹中,我的 readFile 也在其中。

这是重现我的问题的代码:

#include <iostream>
#include <fstream>

void readFile(const std::string& filename) {
    std::ifstream file(filename, std::ios::ate | std::ios::binary);

    if (file.is_open())
        std::cout << "test";
    else
    {
        std::cin.get(); // I always land on the else block
    }
}

int main()
{
    readFile("shader.frag.spv");
}

正在重新启动 visual studio、更改文件名、更改其内容、将其移动到单独的文件夹、使用绝对路径。 None 解决了我的问题。

有什么想法吗?

假设 文件 已经在您当前的 工作目录 中,代码应该可以工作,检查一下!

默认情况下,Visual Studio 中的当前目录是项目根目录,而不是可执行文件所在的位置,如@someProgrammerDude 所述。

如果您不确定,请查看 C++ Visual Studio Current Working Directory