|
Loading strings from files?
I''m making an RPG game and I need to load strings from files.
I just don''t know how it''s done. I can load itegers using sscanf, that''s
easy but how would I load strings.
I read a line like this:
In my map.txt it looks like this:
Name: NAMEOFTHEMAP
How would I get NAMEOFTHEMAP into "char * MapName"?
I really need help on this one?
PS. also loading names with more then one word into one char* would be really great but not nescessary
/MindWipe
"If it doesn''t fit, force it; if it breaks, it needed replacement anyway."
"To some its a six-pack, to me it's a support group."
%s should work with fscanf, but if not compare each new read letter to ''\n'' or EOF. I think you are just comparing the first letter of the string to null. Try fgetc and then keep adding a char onto the end until the char == null
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
The Micro$haft BSOD T-Shirt
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
instead of using the old C way of reading files, you could use the easier C++ way.
ins<<buffer will read in a string until it hits whitespace (space or newline).
cout>>buffer will print the string.
if you use the cstring.h file, you will have to use the cout to print it, printf will not work.
Are you even trying to be intelligent?
'cuz if you are, it ain't workin'
Down with Tiberia!![](http://www.aeon-software.com/misc/notiberia.gif)
Edited by - capn_midnight on April 18, 2001 2:18:50 PM
|
ins<<buffer will read in a string until it hits whitespace (space or newline).
cout>>buffer will print the string.
if you use the cstring.h file, you will have to use the cout to print it, printf will not work.
Are you even trying to be intelligent?
'cuz if you are, it ain't workin'
![](http://www.aeon-software.com/misc/notiberia.gif)
![](http://www.aeon-software.com/misc/notiberia.gif)
Edited by - capn_midnight on April 18, 2001 2:18:50 PM
[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]
Um, prolly the best way, is to use ifstreams like cap'n midnight said, but instead of just using the << operator, do it like this:
char input[80];
ins.get_line( input, 80 );
this will give you the entire line (80 characters) or it will stop earlier if there is a return. You can optionally add a 3rd parameter to stop it at a space, or at a ';' (comments?) etc. If you like I can send you some more code... just mail me.
freshya.
[EDIT]sorry I didn't mean the using strings is bad, but if you want to use standard char arrays, which is what MindWipe used in his example, then that is the best way... And if you have a standard null terminated string, then you can use it for all sorts of things, like printf, and GDI drawing[/EDIT]
Edited by - freshya on May 2, 2001 4:24:25 AM
char input[80];
ins.get_line( input, 80 );
this will give you the entire line (80 characters) or it will stop earlier if there is a return. You can optionally add a 3rd parameter to stop it at a space, or at a ';' (comments?) etc. If you like I can send you some more code... just mail me.
freshya.
[EDIT]sorry I didn't mean the using strings is bad, but if you want to use standard char arrays, which is what MindWipe used in his example, then that is the best way... And if you have a standard null terminated string, then you can use it for all sorts of things, like printf, and GDI drawing[/EDIT]
Edited by - freshya on May 2, 2001 4:24:25 AM
In my map.txt it looks like this:
Name: NAMEOFTHEMAP
Here''s what I''d do.
That ought to work. I think...
Name: NAMEOFTHEMAP
Here''s what I''d do.
|
That ought to work. I think...
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
My general solution to reading or writing any container (including strings) to/from files is this:
write the number of items in the container
write the container
For instance:
This will work in general for most containers. You might want to be careful about the null character at the end of string, not sure if it needs to be appended manually in the istream operator >>.
write the number of items in the container
write the container
For instance:
ostream& operator << (ostream &os, const string &str){ os << str.size (); // output length of string os.write (str.c_str (), str.size ()); // write data return os;}istream& operator >> (istream &is, string &str){ int size; is >> size; // read-in length of string str.resize (size); // set string size is.read (str.begin (), size); // read-in string return is;}
This will work in general for most containers. You might want to be careful about the null character at the end of string, not sure if it needs to be appended manually in the istream operator >>.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement