• 13
• 15
• 19
• 27
• 9

# help - File input from strings

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

## Recommended Posts

I'm trying to open a file from a folder, where the filename is stored in another file (its for a heavily scripted RPG ive posted about a couple of times before here, shameless plug is at the end of the post). Basically im storing the filename as a string, and trying to insert the directory past into the start of it using insert, then trying to open that file. but apparently the open function cant convert from a string to a char *. is there anyway around this or will i have to replace my string with a char array? I thought char arrays were bad and strings were good. anyway, heres some code: actually trying to open the file:
beast_file.open (beastfile.insert(0, "scripts//creatures//"));
Plug: http://www.harrysite.net/forums/viewtopic.php?f=28&t=206&start=0 It actually sort of resembles a game now, like a very basic roguelike. I think its pretty good for not only a first game, but a first real program (excluding things like hello world and a thing which worked out the area and perimiter of a rectangle)

##### Share on other sites
std::string::c_str().

Also, if you make use of the Boost Filesystem library (recommended), you can use the Filesystem file stream classes, which accept std::string objects as arguments.

(Also, use of the file stream open() function is often a red flag. Remember that SC++L file stream objects follow the RAII pattern; you can open the file directly when the object is created by using the appropriate constructor.)

##### Share on other sites
thanks for that, ill look into c_str() and that library. about the other stuff, im a bit of a noob, and it went over my head (eg, whats SC++L? standard c++ library?, whats the RAII pattern?)

##### Share on other sites
Quote:
 Original post by Hernan13thanks for that, ill look into c_str() and that library. about the other stuff, im a bit of a noob, and it went over my head (eg, whats SC++L? standard c++ library?, whats the RAII pattern?)
Yes, the SC++L is the Standard C++ Library.

RAII refers to the 'resource acquisition is initialization' idiom.

##### Share on other sites
I'll look into that opening through the constructor in the future, however atm i have a problem. The filename is defined in another text file, which includes other information, and the way i want the text file organised is like so:
8 * 7 34 beast.txt
(havn't really looked into parsing yet, this method works well for the moment)
it reads the rest of the information fine, but when it tries to read the string, it also reads the whitespace which caused it to stop reading the previous integer. Is there a way to either remove the first value in the string, or make it skip the whitespace before inputting into the string?

edit - on a similar note, if i try to have the filename on the next line to avoid the space, it stays on the previous line and the string ends up being "" (empty).

[Edited by - Hernan13 on July 12, 2008 10:29:42 AM]

##### Share on other sites
Quote:
 it reads the rest of the information fine, but when it tries to read the string, it also reads the whitespace which caused it to stop reading the previous integer. Is there a way to either remove the first value in the string, or make it skip the whitespace before inputting into the string?edit - on a similar note, if i try to have the filename on the next line to avoid the space, it stays on the previous line and the string ends up being "" (empty).
We'll probably need to see some code.

##### Share on other sites
Heres the code which reads the files.
std::ifstream beast_locations;beast_locations.open("scripts//creatures//locations.txt");while (! beast_locations.eof()){        beast_locations >> BeastColour;	beast_locations >> BeastCharacter;	beast_locations >> mi;	beast_locations >> mj;	std::getline(beast_locations, beastfile);	std::ifstream beast_file;	bfilepath = beastfile;	bfilepath.insert(0, "scripts//creatures//");	beast_file.open (bfilepath.c_str());	beast_file.getline(beastname, 16);	std::getline(beast_file, beastname);	beast_file >> mst >> mpe >> men >> mch >> min >> mag >> mlu >> mfer;	beast_file.close();	beastlist.push_back(NULL);	beastlist.back() = new Monster(beastname, beastfile, mi, mj, BeastColour, BeastCharacter);	beastlist[beastcounter]->setstats(mst, mpe, men, mch, min, mag, mlu, mfer);	beastcounter++;}beast_locations.close();

locations.txt example:

8 * 7 34 beast.txt
8 * 9 20 beast.txt

beast.txt:
Beast
20 80 20 10 2 50 7 10

checking values of this in the debugger, if i run it until it's created the first Monster, beastfile is " beastfile.txt", which it obviously cannot open.

note: i realise it's bad to have "beast_file" and "beastfile", and will fix that up, however, thats not whats causing the problem.
Looking at the preview of this post, it doesnt seem to include the ++ after beastcounter, which makes that line useless