g++ istream::readsome trouble

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morten Rodal

    g++ istream::readsome trouble

    #include <iostream>
    #include <fstream>

    using namespace std;

    #ifndef BUFSIZ
    #define BUFSIZ 1024
    #endif

    int main(int argc, char **argv) {
    int length, chunksize, offset = 0;
    char *buf = new char[BUFSIZ];

    ifstream is;
    is.open(argv[1], ios::binary);
    if (!is)
    return 1;

    is.seekg(0, ios::end);
    length = is.tellg();
    is.seekg(offset , ios::beg);

    while (offset < length) {
    #if THIS_WORKS
    is.read(buf, BUFSIZ);
    offset += chunksize = is.gcount();
    #else
    offset += chunksize = is.readsome(buf , BUFSIZ);
    #endif
    cout << "CHUNKSIZE: " << chunksize <<
    ", OFFSET:" << offset << ", LENGTH:" << length << endl;
    }
    return 0;
    }

    With g++ 3.3.3 on Freebsd and Linux or 3.2.2 on SGI Irix read files
    properly. On Linux (Gentoo distribution) the chunksize is always zero,
    but on FreeBSD it is able to read some bytes but suddenly stops.
    Compiling the same program with Intel's C++ compiler (icpc) on FreeBSD or
    Linux makes the program work just fine. Using SGI's MIPSpro compiler also
    works.

    Any ideas where to look?

    --
    Morten Rodal


  • Jeff Schwab

    #2
    Re: g++ istream::readso me trouble

    Morten Rodal wrote:[color=blue]
    > #include <iostream>
    > #include <fstream>
    >
    > using namespace std;
    >
    > #ifndef BUFSIZ
    > #define BUFSIZ 1024
    > #endif
    >
    > int main(int argc, char **argv) {
    > int length, chunksize, offset = 0;
    > char *buf = new char[BUFSIZ];
    >
    > ifstream is;
    > is.open(argv[1], ios::binary);
    > if (!is)
    > return 1;
    >
    > is.seekg(0, ios::end);
    > length = is.tellg();
    > is.seekg(offset , ios::beg);
    >
    > while (offset < length) {
    > #if THIS_WORKS
    > is.read(buf, BUFSIZ);
    > offset += chunksize = is.gcount();
    > #else
    > offset += chunksize = is.readsome(buf , BUFSIZ);
    > #endif
    > cout << "CHUNKSIZE: " << chunksize <<
    > ", OFFSET:" << offset << ", LENGTH:" << length << endl;
    > }
    > return 0;
    > }
    >
    > With g++ 3.3.3 on Freebsd and Linux or 3.2.2 on SGI Irix read files
    > properly. On Linux (Gentoo distribution) the chunksize is always zero,
    > but on FreeBSD it is able to read some bytes but suddenly stops.
    > Compiling the same program with Intel's C++ compiler (icpc) on FreeBSD or
    > Linux makes the program work just fine. Using SGI's MIPSpro compiler also
    > works.
    >
    > Any ideas where to look?
    >[/color]

    The "readsome" function only reads whatever input happens to be
    available in the streambuffer. It's intended for non-blocking input.
    If there's no input immediately available to be read, none will be read,
    and your chunksize will be zero for (effectively) ever. In this case,
    you don't seem to need it; stick with the read/gcount method you
    labelled THIS_WORKS.

    Comment

    • Morten Rodal

      #3
      Re: g++ istream::readso me trouble

      On Mon, 19 Apr 2004 08:50:25 -0400, Jeff Schwab wrote:[color=blue]
      > The "readsome" function only reads whatever input happens to be
      > available in the streambuffer. It's intended for non-blocking input.
      > If there's no input immediately available to be read, none will be read,
      > and your chunksize will be zero for (effectively) ever. In this case,
      > you don't seem to need it; stick with the read/gcount method you
      > labelled THIS_WORKS.[/color]

      Ok. Somehow that isn't written in the docs I found at www.cplusplus.com.
      I think I'll go look for a better source for C++ documentation.

      --
      Morten Rodal


      Comment

      • Jeff Schwab

        #4
        Re: g++ istream::readso me trouble

        Morten Rodal wrote:[color=blue]
        > On Mon, 19 Apr 2004 08:50:25 -0400, Jeff Schwab wrote:
        >[color=green]
        >>The "readsome" function only reads whatever input happens to be
        >>available in the streambuffer. It's intended for non-blocking input.
        >>If there's no input immediately available to be read, none will be read,
        >>and your chunksize will be zero for (effectively) ever. In this case,
        >>you don't seem to need it; stick with the read/gcount method you
        >>labelled THIS_WORKS.[/color]
        >
        >
        > Ok. Somehow that isn't written in the docs I found at www.cplusplus.com.
        > I think I'll go look for a better source for C++ documentation.
        >[/color]

        Here's where I learned about readsome:

        _The C++ Standard Library: A Tutorial and Reference_
        Nicolai M. Josuttis
        page 609

        Comment

        Working...