How to read a binary file larger than 2GB on a 32-bit system

C

cooldisk

Is it possible at all to read 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."

Help? Thanks!
-Joe
 
R

red floyd

Is it possible at all to read 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."

I'm afraid that's off-topic here. You will need to use
platform-specific APIs.

Ask yourself "Would the answer be the same if I changed languages?" If
so, then you're platform or algorithm specific, rather than C++ specific.

Similarly, ask, "Does this question even make sense if I change
platforms?" If not, then you're platform specific.

Your question fails both tests, and in fact, your post makes it clear
it's platform specific (32-bit system).

See http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9 for
list of possible newsgoups.
 
V

Victor Bazarov

Is it possible at all to read a binary file larger than 2GB on a 32-
bit system?

Depends on the system and the C++ library implementation.
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."

Help? Thanks!

You have to query the system for the reasons. I am not sure how to do
that portably. And keep in mind that the implementation you're using
may not be capable of dealing with large files. If that's so, you will
have to resort to the OS-specific ways, for example, on Windows there
are 'CreateFile' and 'ReadFile' API calls.

V
 
C

cooldisk

You are right. I have copied my post to comp.unix.programmer. Sorry
(it seems I cannot remove my post)!

-Joe
 
S

Scott Gifford

Is it possible at all to read a binary file larger than 2GB on a 32-
bit system? I tried the following:

One trick I've used to do this is to read it from a pipe:

cat file |./your_program

This will work fine with a text file (at least with any reasonable
version of cat).

The reason large files require special support is because the seek
pointer needs to be large enough to address the entire file. But
since pipes aren't seekable, this restriction doesn't apply.

Good luck!

----Scott.
 
C

Colin Song

Is it possible at all to read 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."

Help? Thanks!
-Joe



in linux after 2.4.18 you can open the file with O_LARGEFILE flag,and
use lseek,read...
use "man lseek" to see the details
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top