g++ istream::readsome trouble

M

Morten Rodal

#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?
 
J

Jeff Schwab

Morten said:
#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?

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.
 
M

Morten Rodal

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.

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.
 
J

Jeff Schwab

Morten said:
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.

Here's where I learned about readsome:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top