Vector <string> load from file
From: Stephen (pingstephen_at_coldmail.com)
Date: 01/30/04
- Next message: Leor Zolman: "Re: Vector <string> load from file"
- Previous message: Karl Heinz Buchegger: "Re: stl vector find."
- Next in thread: Leor Zolman: "Re: Vector <string> load from file"
- Reply: Leor Zolman: "Re: Vector <string> load from file"
- Reply: Jerry Coffin: "Re: Vector <string> load from file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Jan 2004 14:25:08 GMT
The following code works for me. It puts a vector out into a disk file. I
can then use this file later in the program and fill another vector with the
strings. Notice I load the vector back into the program using getline(). I
wonder if this is the way to do it. Perhaps I should be de-referencing an
iterator instead?
Here's some stripped down code:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
int main()
{
ofstream ofs("content.les");
ostream_iterator<string> countIt(ofs, "\n");
string zonk[10] =
{
"Lucy likes Windows Server",
"Charlie Brown likes Windows 95",
"Snoopy likes Windows 98",
"Sally like Windows Millennium",
"Pigpen dislikes Linux",
"Marcie likes Windows 98 Second ed.",
"Linus likes Lucy",
"Woodstock likes Snoopy",
"Scheroder dislikes Mac OSX",
"Peppermint Patty likes Longhorn betas"
};
for(int i = 0; i < 10; i++)
{
/* ++countIt and countIt++ seem to work the same and I do not understand why
*/
++countIt;
*countIt = zonk[i];
}
ofs.close();
ifstream ifs("content.les");
string zink[10];
for(int i = 0; i < 10; i++)
{
string bean = ""; // Initializing it to an empty string made all the
difference!
getline(ifs, bean);
zink[i] = bean;
cout << zink[i] << endl;
}
cout << endl << endl;
ifs.close();
return 0;
}
-- Swap hot for cold to email me.
- Next message: Leor Zolman: "Re: Vector <string> load from file"
- Previous message: Karl Heinz Buchegger: "Re: stl vector find."
- Next in thread: Leor Zolman: "Re: Vector <string> load from file"
- Reply: Leor Zolman: "Re: Vector <string> load from file"
- Reply: Jerry Coffin: "Re: Vector <string> load from file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|