C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help storing file to vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Tom
Guest





PostPosted: Sun Dec 14, 2003 1:57 am    Post subject: Need help storing file to vector Reply with quote



How can you do this in STL, I need to do this in win32 and would like
this to store each getline() function of the fstream to a vector and
each element of the vector would contain a line. Please can someone
make or give an example program?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
John Potter
Guest





PostPosted: Sun Dec 14, 2003 11:14 am    Post subject: Re: Need help storing file to vector Reply with quote



On 13 Dec 2003 20:57:20 -0500, [email]snyp_ (AT) hotmail (DOT) com[/email] (Tom) wrote:

Quote:
How can you do this in STL, I need to do this in win32 and would like
this to store each getline() function of the fstream to a vector and
each element of the vector would contain a line. Please can someone
make or give an example program?

Here is one way to do it.

struct Line {
string s;
operator string () const { return s; }
};
istream& operator>> (istream& is, Line& s) {
return getline(is, s.s);
}
int main () {
istream_iterator<Line> b(cin), e;
vector<string> v(b, e);
// ...
}

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jon Bell
Guest





PostPosted: Sun Dec 14, 2003 11:15 am    Post subject: Re: Need help storing file to vector Reply with quote



In article <78d6f444.0312131243.7285c56b (AT) posting (DOT) google.com>,
Tom <snyp_ (AT) hotmail (DOT) com> wrote:
Quote:
How can you do this in STL, I need to do this in win32 and would like
this to store each getline() function of the fstream to a vector and
each element of the vector would contain a line. Please can someone
make or give an example program?

What part of it are you having trouble with? Show us what you've tried,
and someone will probably be glad to help out.

--
Jon Bell <jtbellap8 (AT) presby (DOT) edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sun Dec 14, 2003 3:33 pm    Post subject: Re: Need help storing file to vector Reply with quote

In article <78d6f444.0312131243.7285c56b (AT) posting (DOT) google.com>, Tom
<snyp_ (AT) hotmail (DOT) com> writes
Quote:
How can you do this in STL, I need to do this in win32 and would like
this to store each getline() function of the fstream to a vector and
each element of the vector would contain a line. Please can someone
make or give an example program?


std::vector<std::string> store;
std::fstream infile("filename");
std::string s;
while(getline(infile, s)) store.push_back(s);

plus some polish.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.