Ifstream Always Failing To Open A File

I'm currently working on a game that essentially uses ASCII characters to create the level for a top down RPG. I ran into an issue with a function meant to read the contents of a file meant to contain the level format, set each line of the file equal to an array that holds strings of the level data, and uses another function to print those lines to the Command Prompt. Upon attempting to open the file, the program always prints an error message instead of the file contents. I tried running the program again with the error message commented out of the program and nothing showed up on the Command Prompt. Below are the files involved in this program that are relevant to the problem. No other files should be relevant because they currently are never used by the program.(It's hard to implement code for a game if the level won't load.)


main.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "GameExecution.h"
using namespace std;

int main(){
    GameExecution gameExecution;

    gameExecution.levelLoad();

return 0;}


GameExecution.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef GAMEEXECUTION_H
#define GAMEEXECUTION_H
#include <string>
#include <vector>
using namespace std;

class GameExecution{
    public:
        GameExecution();//default constructor, currently does nothing
        void levelLoad();//function to load level 0
        void levelPrint();//function reads current level configuration and prints it to the screen
    private:
        vector<string> _levelLine;//vector containing every line of the current level
        string _line;//variable used to store a single line in the level
};
#endif // GAMEEXECUTION_H 


GameExecution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "GameExecution.h"
#include <iostream>
#include <cstdio>
#include <fstream>


GameExecution::GameExecution(){}

void GameExecution::levelLoad(){
    ifstream levelIn;//input from the level file

    levelIn.open("J;\\...Level0.txt");

if(levelIn.fail()){ //this is currently bugged and always fails to read.
  cout<<"File reading failed. Does this file exist?"<<endl; //error message
  return;}//failing to read the file returns user to main function
else{

while(getline(levelIn, _line)){
    _levelLine.push_back(_line);} //contents of the line are added to the vector as a string element.
    }
    levelIn.close();} //closes the file in case it wasn't already closed.

void GameExecution::levelPrint(){

for(int i = 0; i < _levelLine.size(); i++){
    printf(_levelLine[i].c_str(), "\n");} //function prints each line of the level.
    printf("\n"); //extra new line at the end for better formatting
}
J;\\...Level0.txt is this the proper file location? Also you can simply do if(!levelIn) I would also suggest instead of using .open you do ifstream levelIn("filename.txt"); which is the constructor. By the way why do you include iostream but then use printf instead of cout? *edit actually why do you have printf and cout in different spots?
1
2
3
4
5
6
cout<<"File reading failed. Does this file exist?"<<endl;

...

printf(_levelLine[i].c_str(), "\n");}
    printf("\n"); 
Last edited on
I also included <cstdio>, which uses printf. I generally try to use printf because I've heard that it's faster than cout. Sure, it may not make much of a difference now, but my hopes is the practice will make larger programs run more efficiently.

As for what you said about the file, I thought that using a relative path might deter that issue. I'm going to try with an absolute path and see if the bug still remains.

Before I do, I'd like to ask why you would suggest using your alternative to .open and using if(!levelIn) instead of what I am currently using?
For the alternative to .open:
Currently, you are creating an empty ifstream object and then opening a file for that object. With the alternative, you would be creating an already open ifstream object.
So the reason it's not working is because it's creating an empty object and the alternative basically makes an object with the contents of the file?
No, this alternative isn't what's causing your program to not work. It'll just be faster and more efficient than what you're doing.
How would I get what I'm currently trying to do to work then?
Why does your filename/path include a semi-colon where a colon would be expected?
Because I appear to have not noticed that. I don't think it would've worked regardless. When I changed it to an absolute path, I typed a colon in it's place.
It appears fairly obvious that if the file cannot be opened, it isn't at that path. If you're on windows, you might check to make sure the file extension isn't showing if you have the option enabled to hide them (which it is by default.) Your file might actually be named "xxx.txt.txt" (which would show as "xxx.txt" in a listing with file extensions hidden.) Whether that's the case or not, you need to take a careful look at how the path you're supplying differs from the path the file is actually at.
Thanks for the advice. I've checked that the file extension was not incorrect, and it was indeed a .txt file and not a .txt.txt file by mistake.
I also included <cstdio>, which uses printf. I generally try to use printf because I've heard that it's faster than cout.

I really suggest you stick with cout especially since you don't really appear to be using printf() properly anyway. I really doubt you'll notice much difference in speed. Reading and writing to the console is usually a very "slow" operation to begin with, because you're usually waiting for the user to do something.

Have you tried creating a file for output? Change the name of the file to something like Level0.crp and try to open it as output. By opening the file for output you can then verify that the path is correct and then you can look for this output file and insure you're input files are in the same directory.

Topic archived. No new replies allowed.