Re: seeekg/tellg not working properly?

Discussion in 'C++' started by John Harrison, Oct 18, 2004.

  1. "Randy Yates" <> wrote in message
    news:...
    > I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos),
    > then
    > the next getline() does NOT get from the original position. I've tried
    > doing
    > a clear() before the seekg() to no avail. In essence I've made my own
    > "GetLine"
    > and "PeekLine" functions for my class but due to this problem its not
    > working
    > properly
    >
    > void BOOK::GetLine(string &line)
    > {
    > char linechar[MAX_LINE_SIZE];
    >
    > line.erase();
    > inpFile.getline(linechar, MAX_LINE_SIZE);
    > line.assign(linechar);
    > lineNo++;
    > };
    >
    > void BOOK::peekLine(string &line)
    > {
    > char linechar[MAX_LINE_SIZE];
    > streampos startPos;
    >
    > startPos = inpFile.tellg();
    > inpFile.getline(linechar, MAX_LINE_SIZE);
    > line.erase();
    > line.assign(linechar);
    > inpFile.seekg(startPos);
    > };
    >
    >
    >
    > Am I doing something stupid, or am I justified in getting pissed off? This
    > should
    > have taken about 30 seconds to code and test instead of 1hour+.


    There's nothing I can see wrong with the code above. At least nothing
    relevant to the problem you are describing. The problem is somewhere in the
    code you didn't post.

    If you can post a complete program that demonstrates this problem then
    you'll get whatever it is sorted out very quickly.

    Doing a clear() before the seekg() is a good idea however. It will make your
    function work when the getline() happens to cause an end of file condition.

    john
     
    John Harrison, Oct 18, 2004
    #1
    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. Fred Ma
    Replies:
    2
    Views:
    594
    Fred Ma
    May 24, 2004
  2. Eivind Grimsby Haarr

    The tellg bug

    Eivind Grimsby Haarr, Sep 2, 2004, in forum: C++
    Replies:
    6
    Views:
    931
    Owen Jacobson
    Sep 4, 2004
  3. Catalin Pitis
    Replies:
    0
    Views:
    532
    Catalin Pitis
    Oct 20, 2004
  4. Replies:
    2
    Views:
    921
    Moonlit
    Sep 6, 2005
  5. peek() and tellg()

    , Sep 28, 2005, in forum: C++
    Replies:
    9
    Views:
    586
    P.J. Plauger
    Oct 2, 2005
Loading...

Share This Page