Sign in to follow this  
Sebuc

fstream in windows xp...

Recommended Posts

I am making a console text adventure and whenever I use fstream to open a file using fin.open("game.txt"); windows xp can't open the file, but when I run it in windows 98 the program goes without a hitch. I've already found threads in other forums about this, but no one's answered them yet... Any ideas?

Share this post


Link to post
Share on other sites
heh, I guess I can add a few details :) here's the whole program:

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define GAME_FILE "world.txt";
#define STILL_PLAYING 1;
#define QUIT 0;

int i,j;
string strTemp = "";
struct sRoom
{
string strLocation;
string strRoom[4][2];
string strItem[10];
string strDescription;
};

struct sPlayer
{
int Health;
int Weapon[1][10];
int Item[1][10];
};

void DisplayRoom(sRoom &room)
{
cout << room.strDescription << endl << endl;
}

void GetRoomInfo(ifstream &fin, sRoom &room)
{
for(i=0; i<=3; i++)
{
for(j=0; j<=1; j++)
{
room.strRoom[i][j]= "";
}
}
room.strDescription= "";
i=0;
string strLine = "";

string strRoom = "#" + room.strLocation;

fin.seekg(NULL,ios::beg);
fin.clear();

while(getline(fin,strLine, '\n'))
{
if(strLine == strRoom)
{
while(i<=3)
{
fin >> strTemp >> room.strRoom[i][0];
fin >> strTemp;
while(strTemp != ";")
{
room.strRoom[i][1]=room.strRoom[i][1] + strTemp;
fin >> strTemp;
}
i++;
}
fin >> strTemp;
while(strTemp != ";")
{
room.strDescription=room.strDescription + strTemp;
fin >> strTemp;
}
fin >> strTemp >> strTemp;
while(strTemp != ";")
{
room.strItem[i]= strTemp;
fin >> strTemp;
i++;
}
return;
}
}
}

void Move(ifstream &fin, sRoom &room, string strRoom)
{
if(strRoom == "NONE")
{
cout << "You can't go that way, nerd!" << endl;
return;
}

room.strLocation = strRoom;
GetRoomInfo(fin, room);
DisplayRoom(room);
}

int GetInput(ifstream &fin, sRoom &room)
{
string strInput = "";
cout << endl << ": ";

cin >> strInput;
if(strInput == "look")
{
DisplayRoom(room);
}
else if(strInput == "north")
{
Move(fin, room, room.strRoom[0][1]);
}
else if(strInput == "east")
{
Move(fin, room, room.strRoom[1][1]);
}
else if(strInput == "south")
{
Move(fin, room, room.strRoom[2][1]);
}
else if(strInput == "west")
{
Move(fin, room, room.strRoom[3][1]);
}
else if(strInput == "quit")
{
return 54;
}
else if(strInput == "help")
{
cout << endl << "Commands: look north east south west quit help" << endl;
}
else
{
cout << endl << "Huh???" << endl;
}
return STILL_PLAYING;
}

int main()
{
string quit="";
ifstream fin;
sRoom room;

fin.open("world.txt");
if(fin.fail())
{
cout << "unable to find world file" << endl;
while(quit != "y")
{
cin >> quit;
}
return -1;
}

fin >> strTemp;
while(strTemp != "*")
{
if(strTemp == "Location=")
{
fin >> strTemp;
room.strLocation = strTemp;
break;
}
}

GetRoomInfo(fin, room);
DisplayRoom(room);
while(1)
{
if(GetInput(fin, room) == 54)
{
while(quit != "y")
{
cin >> quit;
}
break;
}
}
fin.close();
return 0;
}


the file world.txt is in the same directory as the program. The problem is, fin.fail() returns a true value. This is a slight modification from the tutorial at gametutorials.com

Share this post


Link to post
Share on other sites
I just run the compile and run option in dev c++, and all of my project files are in the same directory; c:\c++\blarny (yes, my project really is named blarny)

Share this post


Link to post
Share on other sites
Are you sure you named the file correctly and put it in the same directory as the executable?
I am running WinXP Pro and i tried your code...it works as i think it should. Hard to say when i don't have the "world.txt" but i created on and simply put an "*" inside it, and i get to the command line where i can type "east, north, quit.." and so on...

And...should really the lines with #define end with a semicolon? MVC++ complained on them and i can't remember i have ever used semicolon on #defines...

Share this post


Link to post
Share on other sites
You should not use semicolons at the end of defines. It is not necessary, and it will probably cause a lot of problems if you do.

I've heard of a problem in dev-c++ that uses a working directory when you compile and run, which is not the same as the directory of the executable. Try compiling the program, and running the .exe that it generated in your folder. It should then see the files in that directory.

Share this post


Link to post
Share on other sites
IIRC, the problem is that when you run your program in Dev-C++ it doesn't cd into the directory first. I recall having a few problems with this my self where textures wouldn't load. The solution is to run it manually as you would normally run a program.

Share this post


Link to post
Share on other sites
Thanks for your help, but i've decided to work on windows 98. I'm still not sure why it wouldn't work on XP, I got rid of the semicolon at the end of the define and double checked the directory path (spelling out the whole directory in the program gave me excape sequence errors), but to no avail.

Share this post


Link to post
Share on other sites
If you write out a full path (like C:\path\to\file.txt) in code, you need to use double-backslashes. The backslash is normally a signal for an escape sequence, like \r or \n. In order to produce an actual backslash, use \\, as in C:\\path\\to\\file.txt.

Share this post


Link to post
Share on other sites
I suggest you take ApochPiQ's advice, but keep using XP since i had no problems compiling your program using it...should work, no..? :)
Did you ever try to run the .exe from the same directory as your textfile?

Share this post


Link to post
Share on other sites
Heh, i forgot about the double backslash thanks for the reminder [grin]. When i run the exe from the directory it runs perfectly!! But that's because i fixed my first loop
fin >> strTemp;
while(strTemp != "*")
{
if(strTemp == "Location=")
{
fin >> strTemp;
room.strLocation = strTemp;
break;
}
fin >> strTemp;
}

so that it would actually move on to the next word in the file, instead repeating indefinately...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this