Public Group

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

## Recommended Posts

Hey, I am trying to add a function to my textrpg that allows me to load files containing data for my rooms, thus so I can fix typos and such without recompiling. To save 200+ lines of repetitive code, I wish to simply change the address of my loader in game. I thought of doing it like below, but that doesn't work.
string Address = "C:\\...\\Rooms\\Room01"
if(! file)
{
cout << "Error opening file! Exiting.\n";
return -1;
}
while(!file.eof())
{
getline(file, Room[01]); //Room[01] is a string array
}
file.close();
What is the correct way of doing this? [Edited by - Servant of the Lord on March 17, 2006 6:09:12 PM]

##### Share on other sites
You're not saying how this fails. Does it fail to compile, or fail to run? If it fails to run, can you step through it in the debugger and check all the arguments? For example, if you have an array of strings, you may have to pre-allocate the array (and strings) before reading into them.

##### Share on other sites
For some unfathomable reason ifstream does not have a constructor accepting a string, only a char const *. Change the file definition to:

Σnigma

##### Share on other sites
edit: Ok, thanks Enigma.

##### Share on other sites
Wait, new question!

Your code works fine with my prog, but it doesn't load! The error catcher trips and I get the "Error opening file! Exiting." Line. It seems obvoius that I got my address wrong and there's no file at the location it heads to. Heres my address:
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Mist Valley\Rooms

Yeah, pretty long aint it? Anyhow, in the 'Rooms' folder there is a .txt file named 'Room01' that I wish to load. So I have in my address string this address:

C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Mist Valley\Rooms\Room01

I insert double backslashes so it doesn't pick "'\t'ext based Rpgs" as a command. My address now looks like:

C:\\My Documents\\C++ Programs\\My Programs\\Text based Rpgs\\Mist Valley\\Rooms\\Room01

It trips up. I now realize that my string is probably cutting off everything past the first space, I swap the spaces for underscores; so my address now is:

C:\\My_Documents\\C++_Programs\\My_Programs\\Text_based_Rpgs\\Mist_Valley\\Rooms\\Room01

Still trips up! What should I do? Thanks!

[Edit:] Tried '/' instead of '\'; Doesn't work either. Maybe it isn't the address? Any help is appreciated!

##### Share on other sites
well.... the file name is Room01.txt not just Room01. So your address string should contain the full file name: "Room01.txt"

also, just use forward slash:

C:/My Documents/C++ Programs/My Programs/Text based Rpgs/Mist Valley/Rooms/Room01.txt

-me

##### Share on other sites
Quote:
 Original post by Palidinewell.... the file name is Room01.txt not just Room01. So your address string should contain the full file name: "Room01.txt"also, just use forward slash:C:/My Documents/C++ Programs/My Programs/Text based Rpgs/Mist Valley/Rooms/Room01.txt-me

*groan* How could I have forgotten that? I even made a liitle comment line telling me to add it in the string! I guess I should start reading those lines...

Works fine now; thanks.

man i'm stupid today

##### Share on other sites
just turn on file extensions in windows explorer so you always see files with their extensions. that was you'll start thinking of the files as they actually appear rather than as the dumbed down version that windows shows you.

-me

##### Share on other sites
My advice is to keep the save file in the same folder as your executable, or one folder away.
Also I think you should pass Room01.txt instead of Room01 and those slashes shouldn't be like that. I use /.../.../ and it works.

: way too slow, but I made some checking to make sure I'm not posting garbage.

[Edited by - Calin on March 17, 2006 5:33:07 PM]

##### Share on other sites
Quote:
 Original post by Palidinejust turn on file extensions in windows explorer so you always see files with their extensions. that was you'll start thinking of the files as they actually appear rather than as the dumbed down version that windows shows you.-me

I have on extension viewing, and I have created loading files before; but in a state of pure moronism I forgot to add it. Thanks though.

Quote:
 Original post by CalinMy advice is to keep the save file in the same folder as your executable, or one folder away.Also I think you should pass Room01.txt instead of Room01 and those slashes shouldn't be like that. I use /.../.../ and it works.

I normally have it in the same folder, but for this project I want one for each room so I created a sub-folder in my project folder. So yeah, one folder away.
Does /.../.../.../ work? I tried it after I updated my project, and it didn't.
I believe I read somewhere that linux uses /.../ whilst windows uses '\...\...\'.

Also, I have double slashes because C++ reads \t from

C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Mist Valley\Rooms\Room01

as a tab; and tries to get commands from the other slashes as well unless double slashed. Thanks for your input though.

Thanks all!

• 10
• 17
• 9
• 13
• 41