Re: seeekg/tellg not working properly?

Discussion in 'C++' started by Catalin Pitis, Oct 20, 2004.

  1. "Mike Wahler" <> wrote in message
    news:aNDcd.6273$...
    >
    > "Randy Yates" <> wrote in message
    > news:...
    >> Malte Starostik <> writes:
    >>
    >> > Randy Yates schrieb:
    >> >> void BOOK::GetLine(string &line)
    >> >> {
    >> >> char linechar[MAX_LINE_SIZE];
    >> >> line.erase();
    >> >> inpFile.getline(linechar, MAX_LINE_SIZE);
    >> >> line.assign(linechar);
    >> >> lineNo++;
    >> >> };
    >> >
    >> > FWIW, there's std::getline that takes a std::string as an argument, no
    >> > need for hardcoded length limits and a char array:
    >> >
    >> > using namespace std;
    >> >
    >> > void BOOK::GetLine(string &line)
    >> > {
    >> > getline(inpFile, line);
    >> > ++lineNo;
    >> > }
    >> >
    >> > Regards,
    >> > Malte

    >>
    >> Thanks Malte, I'll keep that possibility in mind.

    >
    > But note that 'getline()' depends upon newline characters,
    > which can require translation (via a 'text mode' stream)(*),
    > and that in 'text mode', 'seek()' and 'tell()' functions
    > can never be guaranteed to have a byte-level precision.
    >
    > (*) Some platforms externally represent '\n' with more than
    > a single character. In the C++ memory model, '\n' is
    > always a single character. (Ain't I/O fun? :) )


    I had that problem once. I solved it by using the "extended" overload of
    getline. I opened the input stream as binary stream and I explicitly
    specified the eol character ('\x0A'). For what I had to solve, it was ok.

    Catalin
     
    Catalin Pitis, Oct 20, 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:
    617
    Fred Ma
    May 24, 2004
  2. Eivind Grimsby Haarr

    The tellg bug

    Eivind Grimsby Haarr, Sep 2, 2004, in forum: C++
    Replies:
    6
    Views:
    946
    Owen Jacobson
    Sep 4, 2004
  3. John Harrison
    Replies:
    0
    Views:
    502
    John Harrison
    Oct 18, 2004
  4. Replies:
    2
    Views:
    958
    Moonlit
    Sep 6, 2005
  5. peek() and tellg()

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

Share This Page