Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

read every other string from a text file


Hi,
I am trying to read every other string from a text file and store it
in a vector. I can use istream_iterator to read all strings and store
it in a vector. However, I only need the first, third,5th etc string
in a vector. How do I do this?
Thanks a lot. Any help I can get is appreciated

aarth@gmail.com wrote:
> I am trying to read every other string from a text file and store it
> in a vector. I can use istream_iterator to read all strings and store
> it in a vector. However, I only need the first, third,5th etc string
> in a vector. How do I do this?

In a loop, maybe?  Like, while the stream is good, read the line, store
it, then read one more, throw it out, repeat...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Here is the part of the code that I'm not sure how to modify. This
reads every word and stores it in the buffer. I need to store only
every alternate word and I don't know how to do that

void read_file(const string file_name, vector<string>& buffer)
{
   ifstream in_file(file_name.c_str());
   if (!in_file)
   {
      return;
   }

   istream_iterator< string > is(in_file);
   istream_iterator< string > eof;
   copy( is, eof, back_inserter(buffer));

Right here 'buffer' contains every word.  What if you just remove
every other word from it here?  You could use another vector, copy
only the evenly-indexed elements from 'buffer' to it (in a loop),
and then swap them (to return the correct one).

> }

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you! I wasn't quite sure how I would do it, but I figured it out
with your help.
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc