reading new line character into a string

K

KK

Dear comp.lang.c++,
The code below reads off strings of a file into a vector.

ifstream srcfile("c:\\test.txt");
istream_iterator<string> strIter(srcfile), eos;
vector<string > strvec(strIter, eos);

say the file has the following data
c:\\test.txt
---------------
this is a test
to capture new line

after executing the above snippet, we have

strvec[] = {"this","is","a","test","to","capture","new","line"}

However, in this process the "new line" character information is lost.
How can I modify above snippet to capture the new line information too?

I want an ouput such that
strvec[] = {...,"test", "\n", "to",...}

Any suggestions,
Thank you
KK
 
V

Victor Bazarov

KK said:
Dear comp.lang.c++,
The code below reads off strings of a file into a vector.

ifstream srcfile("c:\\test.txt");
istream_iterator<string> strIter(srcfile), eos;
vector<string > strvec(strIter, eos);

say the file has the following data
c:\\test.txt
---------------
this is a test
to capture new line

after executing the above snippet, we have

strvec[] = {"this","is","a","test","to","capture","new","line"}

However, in this process the "new line" character information is lost.
How can I modify above snippet to capture the new line information
too?

I want an ouput such that
strvec[] = {...,"test", "\n", "to",...}

You probably want to incorporate the use of 'getline' there somewhere.
Something like

while stream is OK
get the line;
istringstream from the line;
read all words from the istringstream, stuff your vector
if stream is not at the end
stuff \n into the vector
end-while

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top