C++ 如何在visual c+中打开文件+;?
我正在使用visual studio,打开此文本文件时遇到问题。我已将其与所有源代码一起放在文件夹中,但出现“无此类文件或目录”错误。这是我的密码C++ 如何在visual c+中打开文件+;?,c++,visual-c++,fstream,C++,Visual C++,Fstream,我正在使用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 fil
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();
}
您的程序可能不在源代码所在的目录中运行,而是在
Solution\Debug
目录中运行
将相对于此目录的文件路径传递给函数
load_map("..\\Project\\map.map");
或者移动要在其中打开的文件。或者,如果您不确定程序的工作目录在哪里,请提供完整路径
load_map("c:\\Blah\\Blub\\Project\\map.map");
如果文件在输出目录中,您还可以将调试部分中的工作目录更改为
$(OutDir)
,而不是默认的$(ProjectDir)
我将文件复制到调试文件夹中,但得到了相同的结果error@Utsav,从属性设置检查程序实际运行的位置,并将文件放在那里,或者尝试指定完整路径:load_map(“c:\\Blah\\Blub\\Project\\map.map”)代码>打开一个没有路径的文件,给它写一句话,然后关闭它。搜索该文件。这将显示默认目录或文件夹的位置。@πάνταῥεῖ : 创建一个文件是一个简单的实验,总是有效的。我使用了太多的编译器,我经常忘记默认目录在VisualStudio中的位置。这是我使用的一种技术。快速的
load_map("c:\\Blah\\Blub\\Project\\map.map");