ifstream不打开文件 [英] ifstream not opening file
问题描述
在此函数中,我试图打开一个文件,其中包含一组字符,我想分配给我的矩阵数组,但每当我运行这个程序,控制台显示一个错误,说,该文件是不是打开。另一个问题,如果我将该文件添加到我的资源文件夹如何指定访问该文件,而不是我在我的硬盘驱动器的根目录?
In this function i am trying to open a file that contains a set of characters that i want to assign to my matrix array, however whenever i run this program the console displays an error that says that the file is not open. Another question, if i add that file to my resource folder how do i specify to access that file and not the one that i have in the root of my hard drive?
ifstream readSecondMap("C:\\map_2.txt", ifstream::in);
void Stage::populateStage(ifstream &myStage, char (&myArray)[mapXcor][mapYcor]) {
if(myStage.is_open()){
for(int a = 0; a < mapXcor+1; ++a){
for(int b = 0; b < mapYcor+1; ++b){
myArray[a][b] = (char) myStage.get();
}
}
myStage.close();
} else {
std::cout << "Error: Unable to open File" <<std::endl;
}
}
推荐答案
as @TianyunLing注:
Just as @TianyunLing noted:
我在KUbuntu 12.10上测试过:
I've tested on KUbuntu 12.10:
- 打开错误:如果
map_2.txt
不存在,将会发生错误。 - 文件路径:
- Open error: if
map_2.txt
does not exist, the error will occur. - File path:
文件夹1
+ ------- file1
+ ----- - file2
folder2
+ ------- program
+ ------- file3
为您的程序
访问file1,使用../ folder1 / file1
,访问file3
,使用 file3
。 (假设您不改变程序
工作目录)
for your program
to visit file1, use "../folder1/file1"
, to visit "file3"
, use file3
. (suppose that you don't change your program
working directory)
一更多事情,你不需要为 ifstream
指定 ifstream :: in
。
One more thing, you don't need to specify ifstream::in
for ifstream
.
这篇关于ifstream不打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!