fstream in windows xp...

Started by
12 comments, last by Sebuc 19 years, 9 months ago
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?
Advertisement
Have you tried using the old C file IO functions?
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?
神はサイコロを振らない!
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
How do you start your program? What is the current directory, when you start it?
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)
Have you tried to start your program from the commandline when you are in the directory of your files?
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...
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
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.
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.

This topic is closed to new replies.

Advertisement