Advertisement

Saving/Loading more than one string...?

Started by March 04, 2002 07:18 PM
4 comments, last by Scott_U 22 years, 6 months ago
Okay, I know how to save, but when I do it''s all one big line of things. I need to know how to seperate them and then be able to load them (I also know how to load one thing) at the same time. Any and all help is appreciated. Thank you.
Save to file? text file? ascii? Try inserting ''\n'' in between each string (just add it to the end of each string that you want to be on one line). Take a look at fgets for loading the file back up.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
Text, and I''ll try the /n, thanks...
I am assuming you are going to use fstream to save/load your file. LessBread is correct, just input a new line after each peice of data. However, that makes it a little bit more complicated when you go to reinput the data. If you use cin to input a string, it will stop at every space it encounters. Therefore you must use getline(). To input the line, you need to use getline(getdata,varname), where getdata is your fstream set for input from your file and varname is the variable, which in this case is a string.
Getline() will stop when it encounters the newline character, so it is nessarary for you to input a dummy string to get rid of the newline character. If you dont, it will throw off all of your data input (half of them will contain newline characters). After you do this, you will be ready to get the next line of data. Just make sure you input the data in the same order as you wrote. Hope that made sense.

"cogito, ergo sum" -Descartes
"cogito, ergo sum" -Descartes
The /n didn''t work for me. I don''t fully understand what your saying Conun. Maybe if someone could post an exampe or something I''d get it. :-/ Thanks to you two for helping and anyone else that helps.
It''s ''\n'', not ''/n''. Besides, if you want to read a file full of whitespace delimited strings into a C++ program, you can use istream_iterators in conjunction with the STL copy() algorithm. For example:

  #include <vector>#include <iterator>#include <string>#include <iostream>#include <fstream>using namespace std;int main(){ vector<string> v; ifstream in("in.txt"); // Read the file into a vector of strings: copy( istream_iterator<string>(in), istream_iterator<string>(),  back_inserter(v)); // Write the vector of strings to the standard output: copy( v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));}  


--
You see some pale bulbous eyes staring at you.

This topic is closed to new replies.

Advertisement