C++ ifstream not working

Pages: 12
closed account (GbX36Up4)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

void mainmenu();
void loadgame();
void loaddesc();
void readdesc();
void input();

int location = 1;
string text;
string desc;
int beenthere = 0;

int main(){
    int gameon = 0;
    if (beenthere == 0){
    mainmenu();
    }
    if (beenthere == 1){
        while(gameon == 0){
            loaddesc();
            readdesc();
            input();
            system("pause");
        }
            return 0;
    }
}

void mainmenu(){ // where the game starts
    START:
int option;
cout << "Would you like to create a new game or load from a previous one?" << endl;
cout << "[1]New Game" << endl;
cout << "[2]Load Game" << endl;
cin >> option;
    if(option == 1){beenthere = 1; main();}
    if(option == 2) loadgame();
    else goto START;
}

void loadgame(){
}

void loaddesc(){
     { switch(location){
        case 1: ifstream in("one.txt"); if(!in){cout << "Cannot open file." << endl;}  in >> desc; in.close(); break;
       }
     }
}

void readdesc(){
    cout << desc << endl;
}

void input(){

}



It cannot open the file for some reason. What am I doing wrong? Thanks.
Last edited on
What am I doing wrong?


I suspect the file is not in the directory your program is looking in. Try using the full path name; for example,

ifstream in("C:/someDirectory/andSomeOtherDirectory/one.txt");

Note that you can use the forward-slash in that line, even under windows.
closed account (GbX36Up4)
It still does not work. ):
Does the file exist, and in which directory?
Last edited on
closed account (GbX36Up4)
Yes. it is in C:/Testing/one.txt Testing is the same folder the program is in.
Try listing "ifstream inFile;" near your other variables before you use it.

It doesn't appear you listed it with the other variables.

Insert ifstream inFile; on line 19, and then give it a try.



If you try it my way, change line 51 to ifstream inFile too or you will get another error.

Further, change all your "in" to "inFile" if you try it my way.

Lastly, using "in" is not very good. You should use something a bit more descriptive and intuitive like "inFile".

Cheers.

Here is a more in-depth response:

1
2
3
4
5
6
inFile.open("Your File Goes Here.txt");
	if(inFile.fail())            //is it ok?
   { cout<<"Input file did not open please check it\n";
   system("pause");
   return 1;
   }
Last edited on
closed account (GbX36Up4)
I now have the code as:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

void mainmenu();
void loadgame();
void loaddesc();
void readdesc();
void input();

int location = 1;
string text;
string desc;
int beenthere = 0;

int main(){
    int gameon = 0;
    if (beenthere == 0){
    mainmenu();
    }
    if (beenthere == 1){
        while(gameon == 0){
            loaddesc();
            readdesc();
            input();
            system("pause");
        }
            return 0;
    }
}

void mainmenu(){ // where the game starts
    START:
int option;
cout << "Would you like to create a new game or load from a previous one?" << endl;
cout << "[1]New Game" << endl;
cout << "[2]Load Game" << endl;
cin >> option;
    if(option == 1){beenthere = 1; main();}
    if(option == 2) loadgame();
    else goto START;
}

void loadgame(){
}

void loaddesc(){
    ifstream inFile;
     { switch(location){
        case 1:  inFile.open("C:\Testing\one.txt"); if(!inFile){cout << "Cannot open file." << endl;}  inFile >> desc; inFile.close(); break;
       }
     }
}

void readdesc(){
    cout << desc << endl;
}

void input(){

}


It still says it can't open the file. :/
This "C:\Testing\one.txt" has to be "C:\\Testing\\one.txt" // Note the '\\' . In C/C++ the '\' introduces an escape sequence. This "\t" for example comes out as a 9 (tab)

Unknown escape sequences are omitted. So your string is compiled as "C:estingne.txt"
That's funny, I could have sworn I made specific reference to using forward slashes.

Try using the full path name; for example,
 
ifstream in("C:/someDirectory/andSomeOtherDirectory/one.txt");


Note that you can use the forward-slash in that line, even under windows.


Oh, I did.
An additional observation... never EVER call main() directly. It is not legal C++.

I suggest replacing the goto's with a proper loop too.

http://www.cplusplus.com/doc/tutorial/control/
You need to put this code on line 19:

ifstream in;


The reason for this is that you also have to declare ifstream in.


You have the correct header #include<fstream> , and you initialize it correctly, too.

But you did not declare ifstream in at line 19.

Declare ifstream in, and it should work.


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

void mainmenu();
void loadgame();
void loaddesc();
void readdesc();
void input();

int location = 1;
string text;
string desc;
int beenthere = 0;

int main(){
    ifstream in;  // look here is the change you need to try
    int gameon = 0;
    if (beenthere == 0){
    mainmenu();
    }
    if (beenthere == 1){
        while(gameon == 0){
            loaddesc();
            readdesc();
            input();
            system("pause");
        }
            return 0;
    }
}

void mainmenu(){ // where the game starts
    START:
int option;
cout << "Would you like to create a new game or load from a previous one?" << endl;
cout << "[1]New Game" << endl;
cout << "[2]Load Game" << endl;
cin >> option;
    if(option == 1){beenthere = 1; main();}
    if(option == 2) loadgame();
    else goto START;
}

void loadgame(){
}

void loaddesc(){
    ifstream inFile;
     { switch(location){
        case 1:  inFile.open("C:\Testing\one.txt"); if(!inFile){cout << "Cannot open file." << endl;}  inFile >> desc; inFile.close(); break;
       }
     }
}

void readdesc(){
    cout << desc << endl;
}

void input(){

}
Last edited on
closed account (GbX36Up4)
I copied your code and also replaced the the \'s with /'s and it still claims it cannot open the file.
Try changing the spelling of the file.

For example:

"One.txt"

or

"ONE.txt"

Also, make sure the input of the file is the correct input. For example, if you want to read numbers, don't include things that are not numbers if you don't have the proper coding to detect things that are not numbers.


C++ will read both of these inputs differently:

1, 2, 3, 4, 5


12345

Try looking at your input for any mistakes. See if that works.
Last edited on
closed account (GbX36Up4)
I changed it to One.txt The file itself contains: You are in a large orange room.
After changing the file name it still does not work.
Try testing the input of the file with just numbers and see what happens.

Try this:

12345

and try this:

1 2 3 4 5

and try this:

1, 2, 3, 4, 5,


Each of these three number combinations are interpreted differently depending on your coding.

closed account (GbX36Up4)
I tried the first one and it cannot open the file.
closed account (GbX36Up4)
Same with the second and third. I don't think it is what is in the file. The file just isn't opening for some reason which is most likely a error in the code but I don't know what since this is my first time using fstream.
Alright, here is another thing:

Ok, I'm assuming that for this program, you have already created a folder that holds the program.

For example, this program is named "VideoGame1" so somewhere in your C drive, I should be able to search for this program.

Now...since you already have a folder named VideoGame1 in your C drive, don't use the entire path to the file you want to open.

For example, don't use this
 
inFile.open("C:\Testing\one.txt");  


Instead, use this:

inFile.open("one.txt");

Make sure the file you want to open is in the folder of the program that you are coding.


Also, did you declare desc?


Where in the coding of this game is the variable desc?

It doesn't look like you declared it. Declare the variable desc and then see what happens.

Never mind, I see you declared it as a string.

This is another problem:

Since you are declaring a string, you need to use the string directive

1
2
#include<string>



Try this and see what happens.
Last edited on
closed account (GbX36Up4)
It still does not work. Here is the current code:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

void mainmenu();
void loadgame();
void loaddesc();
void readdesc();
void input();

int location = 1;
string text;
string desc;
int beenthere = 0;

int main(){
    ifstream in;  // look here is the change you need to try
    int gameon = 0;
    if (beenthere == 0){
    mainmenu();
    }
    if (beenthere == 1){
        while(gameon == 0){
            loaddesc();
            readdesc();
            input();
            system("pause");
        }
            return 0;
    }
}

void mainmenu(){ // where the game starts
    START:
int option;
cout << "Would you like to create a new game or load from a previous one?" << endl;
cout << "[1]New Game" << endl;
cout << "[2]Load Game" << endl;
cin >> option;
    if(option == 1){beenthere = 1; main();}
    if(option == 2) loadgame();
    else goto START;
}

void loadgame(){
}

void loaddesc(){
    ifstream inFile;
     { switch(location){
        case 1:  inFile.open("One.txt"); if(!inFile){cout << "Cannot open file." << endl;}  inFile >> desc; inFile.close(); break;
       }
     }
}

void readdesc(){
    cout << desc << endl;
}

void input(){

}
Alright.

Can you change your input to one word and see if your program will output the word?


Try something simple like


Steven


in your input file, and see what happens.

Also, why do you have

1
2
3
string text;
string desc;


Try using one only and see what happens.

Try
1
2
string desc;
Last edited on
Pages: 12