 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tom Guest
|
Posted: Sun Dec 14, 2003 1:57 am Post subject: Need help storing file to vector |
|
|
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
|
Posted: Sun Dec 14, 2003 11:14 am Post subject: Re: Need help storing file to vector |
|
|
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
|
Posted: Sun Dec 14, 2003 11:15 am Post subject: Re: Need help storing file to vector |
|
|
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
|
Posted: Sun Dec 14, 2003 3:33 pm Post subject: Re: Need help storing file to vector |
|
|
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 |
|
 |
|
|
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
|
|