ifstream not opening file
Solution 1
Just as @TianyunLing noted:
I've tested on KUbuntu 12.10:
- Open error: if
map_2.txt
does not exist, the error will occur. - File path:
folder1
+------- file1
+------- file2
folder2
+------- program
+------- file3
program
to visit file1, use "../folder1/file1"
, to visit "file3"
, use file3
. (suppose that you don't change your program
working directory)
One more thing, you don't need to specify ifstream::in
for ifstream
.
Solution 2
The issue is most likely one of the following:
1) map_2.txt does not exist in the location you specified in your ifstream declaration.
2) You do not have sufficient rights to access the root folder of your C drive.
I advise moving the file to the same folder that your code is stored in temporarily and trying to get it to work with that location first to verify that the issue is with file location or access rights rather than something you're doing in your code. Then move it to your resource folder, and use a relative path name to access it if it works. If it doesn't work when the file is in the same folder as your code and when you have the pathname written correctly, then you are probably doing something wrong in your code and would need to post a larger portion of the code to expose the issue to us.
Daniel Sega
Updated on April 12, 2020Comments
-
Daniel Sega about 4 years
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; } }