Vector <string> load from file

From: Stephen (pingstephen_at_coldmail.com)
Date: 01/30/04


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.


Relevant Pages

  • Re: RM/Cobol - Printing in XP
    ... 'Windows' and I can't use a conventional print to LPTx. ... to write to file and print the disk file subsequently. ... If using 'landscape' with condensed characters (say a line ...
    (comp.lang.cobol)
  • Re: RM/Cobol - Printing in XP
    ... Thank you for the fast response. ... print fine from Windows 98se machines without any issues. ... > problem with any COBOL compiler running under any version of Windows. ... > to write to file and print the disk file subsequently. ...
    (comp.lang.cobol)
  • Re: Migrate PPC --> Intel iMac = Possible? Easy?
    ... Mendelson wrote: ... can import the virtual pc disk file. ... There may be a problem with Windows. ... If the cpu, bus controller, or disk controller ...
    (comp.sys.mac.system)
  • Re: Migrate PPC --> Intel iMac = Possible? Easy?
    ... can import the virtual pc disk file. ... There may be a problem with Windows. ... it when you change hardware. ... If the cpu, bus controller, or disk controller ...
    (comp.sys.mac.system)
  • Re: Hot to change motherboard without reinstalling XP?
    ... Mr Bean wrote: ... You will need to extract files from the Windows XP CD. ... You'll be required to reactivate Windows XP and the Device Manager will be ... data, user settings, Service Pack Updates and Windows Updates will all be ...
    (microsoft.public.windowsxp.hardware)