c++ - 无法从文件中读取
问题描述
在遵循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,更改文件名,更改其内容,将其移动到单独的文件夹,使用绝对路径。这些都没有解决我的问题。
有任何想法吗?
解决方案
鉴于该文件已经在您当前的工作目录中,该代码应该可以工作,请检查!
默认情况下,Visual Studio 中的当前目录是项目根目录,而不是可执行文件所在的位置,正如@someProgrammerDude 提到的那样。
如果您不确定,请检查C++ Visual Studio Current Working Directory。
推荐阅读
- database - 将图像保存在房间数据库中
- php - 在管理产品编辑中访问和编辑 WooCommerce 产品元数据
- artifactory - 删除 6 个月未下载的工件
- google-cloud-platform - Google Cloud Storage 中的 Concat Avro 文件
- linux - 如何使用 docker-compose 使用主机网络配置
- email - 向 exim4 添加自定义标头
- c++ - 为什么即使在使用 constexr if 时我也会得到“使用未声明的标识符”?
- tekton - 使用具有多个部署的多个存储库实施 Tekton 的最佳实践是什么
- c# - 无法使用 Office 互操作签出文件
- javascript - 输入时表单输入未得到验证 (@input),只有 @blur 有效