Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reading new line character into a string

Reply
Thread Tools

reading new line character into a string

 
 
KK
Guest
Posts: n/a
 
      09-13-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-13-2006
KK wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
reading input from a file - changing new line character hollyhawkins Perl Misc 2 12-19-2006 02:39 AM
8 bit character string to 16 bit character string Brand Bogard C Programming 8 05-28-2006 05:05 PM
Insert a new line character into a string literal Water Cooler v2 Javascript 4 04-18-2006 12:02 PM
end-of-line character problem while reading stream characters and wrinting into array dbuser C++ 8 10-10-2005 03:11 AM



Advertisments