From: cooldisk on
Is it possible at all to read (in C++) a binary file larger than 2GB
on a 32-bit system? I tried the following:

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[]) {
fstream fsBin;
fsBin.open(argv[1], fstream::in | fstream::binary);
if (! fsBin) {
cerr << "File " << argv[1] << " cannot be opened.\n";
exit(1);
}
fsBin.close();
return 0;

}

When I ran this program to open a file of 4.4GB size, I got: "File foo
cannot be opened." System information:
$ uname -srp
Linux 2.6.15.6 pentium4

Help? Thanks!
-Joe

From: fjblurt on
On Oct 3, 2:23 pm, coold...(a)gmail.com wrote:
> Is it possible at all to read (in C++) a binary file larger than 2GB
> on a 32-bit system? I tried the following:
>
> #include <iostream>
> #include <fstream>
> using namespace std;
>
> int main(int argc, char *argv[]) {
> fstream fsBin;
> fsBin.open(argv[1], fstream::in | fstream::binary);
> if (! fsBin) {
> cerr << "File " << argv[1] << " cannot be opened.\n";
> exit(1);
> }
> fsBin.close();
> return 0;
>
> }
>
> When I ran this program to open a file of 4.4GB size, I got: "File foo
> cannot be opened." System information:
> $ uname -srp
> Linux 2.6.15.6 pentium4

Your program works fine for me. Try calling perror to see why the
file can't be opened. Perhaps there is some other problem.

What versions of glibc and libstdc++ are you using?

From: Ulrich Eckhardt on
cooldisk(a)gmail.com wrote:
> Is it possible at all to read (in C++) a binary file larger than 2GB
> on a 32-bit system?

Yes, definitely!

> I tried the following:
>
> #include <iostream>
> #include <fstream>
> using namespace std;
>
> int main(int argc, char *argv[]) {
> fstream fsBin;
> fsBin.open(argv[1], fstream::in | fstream::binary);

You could have used ifstream in the first place.

> if (! fsBin) {
> cerr << "File " << argv[1] << " cannot be opened.\n";
> exit(1);
> }
> fsBin.close();
> return 0;
> }
>
> When I ran this program to open a file of 4.4GB size, I got: "File foo
> cannot be opened."

This is not a general C++ problem but rather one with the implementation of
C++ IOStreams.

> System information:
> $ uname -srp
> Linux 2.6.15.6 pentium4

Linux supports large files since quite some time. The other involved
components are glibc and libstdc++/GCC, what versions do those have? Also,
someone mentioned perror(), but that is not generally applicable to C++
IOStreams because those don't use errno either, except perhaps if the GCC
implementation guarantees that as extension.

What I could imagine is that the same macro which switches e.g. fseek()
between 32 and 64 bit implementations in C is used in C++, too, but I'm not
sure there and IMHO that also shouldn't be the case.

Uli