getline to remember location between calls

Discussion in 'C++' started by Gary Wessle, Feb 11, 2007.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    I have a method A which have a loop, in that loop, another method B is
    called, B opens a file and does getline till found something
    interesting, does something with it and return void. then the A loop
    goes on till the next time B is called.

    B does while( getline( ofstraam, line ) )
    I am trying not to getline all over from scratch again every time B is
    called but instead keep getline "ganging" in there waiting for B to be
    called again.

    what do I do, do I pass an open ofstream as B argument?


    thank you

    ****************************************************************

    A()
    {
    while ( flag )
    {
    /* ... */
    B ( file_name, string_to_find, end_point );
    }
    }

    B()
    {
    ifstream ifs( file_name.c_str() );
    while( getline( ifs, line ) )
    {
    // does this line have the end_point then break
    // look for string_to_find and do something.
    }
    // next time B is called with a different end_point that is after the
    // previous end_point, I do not want to start from the first line.
    ****************************************************************
    Gary Wessle, Feb 11, 2007
    #1
    1. Advertising

  2. Gary Wessle wrote:
    > Hi
    >
    > I have a method A which have a loop, in that loop, another method B is
    > called, B opens a file and does getline till found something
    > interesting, does something with it and return void. then the A loop
    > goes on till the next time B is called.
    >
    > B does while( getline( ofstraam, line ) )
    > I am trying not to getline all over from scratch again every time B is
    > called but instead keep getline "ganging" in there waiting for B to be
    > called again.
    >
    > what do I do, do I pass an open ofstream as B argument?


    You could (you mean an ifstream, not ofstream, right?).
    Just remember that you cannot pass it by value, only by
    pointer of by reference.

    HTH,
    - J.
    Jacek Dziedzic, Feb 11, 2007
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    Jacek Dziedzic <> writes:

    > Gary Wessle wrote:
    > > Hi
    > > I have a method A which have a loop, in that loop, another method B
    > > is
    > > called, B opens a file and does getline till found something
    > > interesting, does something with it and return void. then the A loop
    > > goes on till the next time B is called.
    > > B does while( getline( ofstraam, line ) )
    > > I am trying not to getline all over from scratch again every time B is
    > > called but instead keep getline "ganging" in there waiting for B to be
    > > called again.
    > > what do I do, do I pass an open ofstream as B argument?

    >
    > You could (you mean an ifstream, not ofstream, right?).
    > Just remember that you cannot pass it by value, only by
    > pointer of by reference.
    >
    > HTH,
    > - J.


    A()
    {
    ifstream ifs(file_name.c_str());
    while (flag) {
    B(ifs, string_to_find, end_point);
    }
    }

    B(ifstream& r_ifs, ...) {
    std::cout << r_ifs.fail() << " " << r_ifs.is_open() << std::endl;
    while( getline( ifs, line ) )
    {
    // does this line have the end_point then break
    // look for string_to_find and do something.
    }
    }

    is printing

    0 1 for the first pass
    then
    1 1
    from then on.

    why the input stream failed after the first pass, and how can I fix it?
    Gary Wessle, Feb 12, 2007
    #3
  4. Gary Wessle wrote:
    > Jacek Dziedzic <> writes:
    >
    >> Gary Wessle wrote:
    >>> Hi
    >>> I have a method A which have a loop, in that loop, another method B
    >>> is
    >>> called, B opens a file and does getline till found something
    >>> interesting, does something with it and return void. then the A loop
    >>> goes on till the next time B is called.
    >>> B does while( getline( ofstraam, line ) )
    >>> I am trying not to getline all over from scratch again every time B is
    >>> called but instead keep getline "ganging" in there waiting for B to be
    >>> called again.
    >>> what do I do, do I pass an open ofstream as B argument?

    >> You could (you mean an ifstream, not ofstream, right?).
    >> Just remember that you cannot pass it by value, only by
    >> pointer of by reference.
    >>
    >> HTH,
    >> - J.

    >
    > A()
    > {
    > ifstream ifs(file_name.c_str());
    > while (flag) {
    > B(ifs, string_to_find, end_point);
    > }
    > }
    >
    > B(ifstream& r_ifs, ...) {
    > std::cout << r_ifs.fail() << " " << r_ifs.is_open() << std::endl;
    > while( getline( ifs, line ) )
    > {
    > // does this line have the end_point then break
    > // look for string_to_find and do something.
    > }
    > }
    >
    > is printing
    >
    > 0 1 for the first pass
    > then
    > 1 1
    > from then on.
    >
    > why the input stream failed after the first pass, and how can I fix it?


    Because
    while(getline(...))
    instructs the program to read until it fails. This usually means
    that end-of-file is reached (the other option is a read error).

    Thus, you are reading in the whole file in the first pass,
    the stream goes failed after end-of-file and then the second
    pass does nothing (except for printing your debug).

    To make it work, your while loop in B should break upon
    finding the string you are looking for. Then the whole file
    will not be read (unless your string was on the last line)
    and you will be able to continue in the second pass.

    HTH,
    - J.
    Jacek Dziedzic, Feb 12, 2007
    #4
    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. Honne Gowda A
    Replies:
    2
    Views:
    857
    Karl Heinz Buchegger
    Oct 31, 2003
  2. K. Jansma
    Replies:
    7
    Views:
    317
    Duncan Booth
    Feb 21, 2006
  3. Frank Miverk
    Replies:
    0
    Views:
    653
    Frank Miverk
    Apr 18, 2007
  4. JML
    Replies:
    6
    Views:
    448
    James Kanze
    Nov 22, 2007
  5. saiho.yuen
    Replies:
    3
    Views:
    403
    kaeli
    Sep 14, 2004
Loading...

Share This Page