473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

Oct 3 '07 #1
6 3720
co******@gmail.com wrote:
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-lit...t.html#faq-5.9 for
list of possible newsgoups.

Oct 3 '07 #2
co******@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '07 #3
You are right. I have copied my post to comp.unix.programmer. Sorry
(it seems I cannot remove my post)!

-Joe

Oct 3 '07 #4


Oct 3 '07 #5
co******@gmail.com writes:
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.
Oct 4 '07 #6
On Oct 4, 4:54 am, coold...@gmail.com wrote:
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

Oct 4 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
7
by: KantKwitDansin1 | last post by:
I have a file "a.dat" containing 10^4 32 bit binary numbers. I need to read in these numbers and deterimine if they are prime. The primality part I will have to figure out later, but I was...
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
10
by: QQ | last post by:
I am trying to read data from Excel files I use the following codes, however, it didn't work Can anyone tell me what the format excel files have, what I should do to read it from C? Thanks a...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
6
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.