reading new line character into a string

Discussion in 'C++' started by KK, Sep 13, 2006.

  1. KK

    KK Guest

    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
    KK, Sep 13, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Sep 13, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dbuser
    Replies:
    8
    Views:
    504
    Thomas J. Gritzan
    Oct 10, 2005
  2. Andreas Leitgeb
    Replies:
    0
    Views:
    439
    Andreas Leitgeb
    May 15, 2009
  3. Mark Space
    Replies:
    0
    Views:
    471
    Mark Space
    May 15, 2009
  4. hollyhawkins
    Replies:
    2
    Views:
    217
    Tad McClellan
    Dec 19, 2006
  5. Water Cooler v2

    Insert a new line character into a string literal

    Water Cooler v2, Apr 18, 2006, in forum: Javascript
    Replies:
    4
    Views:
    366
    Michael Winter
    Apr 18, 2006
Loading...

Share This Page