如何在Visual C 中打开文件
How can i open a file in visual c++?
我正在使用Visual Studio,并且有问题打开此文本文件,我将其放在文件夹中,并使用所有源代码,但我遇到了一个"无此类文件或目录"错误。这是我的代码
void Game::load_map(const char *filename)
{
int width,height,current;
std::ifstream in(filename);
if(in.fail()){
std::cout << "problem opening the file" <<std::endl;
perror(filename);
}
else
{
in >> width;
in >> height;
for(int i = 0; i < height; i++)
{
std::vector<int> vec;
for(int j = 0; j<width;j++)
{
if(in.eof())
{
std::cout<< "file ends error" << std::endl;;
return;
}
in >> current;
if(current>=0 && current<=1)
{
vec.push_back(current);
}else{
vec.push_back(0);
}
}
map.push_back(vec);
}
}
in.close();
}
这是我称之为此功能的方式:
load_map("map.map");
您编程可能不会在放置源代码的同一目录中运行,而是在SolutionDebug
目录中运行。
要么将相对于此目录的文件路径传递到您的函数
load_map("..\Project\map.map");
或移动要在此处打开的文件。或第三选项,如果您不确定编程的工作目录在哪里,请提供完整的路径
load_map("c:\Blah\Blub\Project\map.map");
相关文章:
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- Visual Studio在尝试读取resource.txt文件时崩溃
- 找不到Linux Visual Studio代码C++文件
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- Visual Studio C++ 它只构建选项卡中显示的文件吗?
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 如何在Visual C+++中从JSON文件中读取节点数据
- 通过命令行在Visual Studio中编译单个独立文件
- 来自Visual Studio中控制台项目的exe文件?
- C++20 使用 Visual Studio 编译模块:不编译或导入 ixx 文件
- 将Visual Studio Code路径设置为.clang_format文件
- 无法在Visual Studio 2019中打开文件
- 区分 32 位和 cl.exe 生成的 64 位 PE 对象文件(Visual Studio C++)
- Unitest 文件 (Visual Studio) 上未解析的外部符号
- 强制链接静态库中的 CPP 文件 (Visual Studio)
- C++ CPP 文件 (Visual Studio) 中实现的模板错误
- 如何读/写INI文件Visual c++6.0(MFC)
- 从文件中读取数据并将其写入其他文件(Visual studio 2012)
- 编译*.Lib文件visual studio
- 在可执行文件(Visual Studio)中嵌入编译器选项的字符串