reading line
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: reading line

  1. #1
    Join Date
    May 2012
    Posts
    19

    Post reading line

    Hi Guys!

    how to get the text from file before any space come

    suppose i have file and it reads


    Code:
    aa    this is a
    bb    this is b
    cc    this is c

    i am only interested in aa, bb, cc not the rest of the text. or in other words ignore the text that is after space.

    so i came up with this code

    Code:
    std::ifstream file("abc.txt");
    std::string str;
    while (file >> str /* read next word */)
    {
    	std::cout << str << std::endl;
    	file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); /* ignore rest of line */
    }

    but there is problem, Is there any such a method in which I can use before or after this file.ignore(), to actually keep the rest of the line? I mean, the rest of the line shall be used.

    Considering that the first column of the text file is an integer, something inside of the while loop like:


    Code:
    value = atoi(str.c_str());
    (if value==1){
        cout <<restOfTheLine<<endl;
    }
    BR

    Ewa

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: reading line

    You could use getline instead
    Code:
    std::ifstream file("abc.txt");
    std::string str;
    std::string restofline;
    
    while (file >> str /* read next word */)
    {
        std::cout << str << std::endl;
        std::getline(file,restofline);
        value = atoi(str.c_str());
        if ( value==1){
            cout <<restofline <<endl;
        }
    }
    Kurt
    Last edited by ZuK; May 3rd, 2012 at 05:26 PM.

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,595

    Re: reading line

    Quote Originally Posted by me_newbie View Post
    i am only interested in aa, bb, cc not the rest of the text. or in other words ignore the text that is after space.

    [...]

    but there is problem, Is there any such a method in which I can use before or after this file.ignore(), to actually keep the rest of the line? I mean, the rest of the line shall be used.
    Errr, now what do you want: discard it or keep it?

    At any rate, this question has nothing to do with C++/CLI, the .NET variant of C++, which the forum section here is about. It would much better be placed in the Non-Visual C++ section. So I suggest you repost it there, unless a moderator spots it first and moves it.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center