Jump to content
  • Advertisement
Sign in to follow this  
Sebuc

fstream in windows xp...

This topic is 5098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
I don't know, I mean, it's kind of hard to say anything with
so little information. [rolleyes]

How about some code to start?

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[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[0];
fin >> strTemp;
while(strTemp != ";")
{
room.strRoom[1]=room.strRoom[1] + strTemp;
fin >> strTemp;
}
i++;
}
fin >> strTemp;
while(strTemp != ";")
{
room.strDescription=room.strDescription + strTemp;
fin >> strTemp;
}
fin >> strTemp >> strTemp;
while(strTemp != ";")
{
room.strItem= 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
Have you tried to start your program from the commandline when you are in the directory of your files?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!