472,353 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

How to open VERY large file using std::ifstream

Dear all,

I am currently writting a trace analyzer in C++.
It always fails to open a very large input file (3.7Gb).

I tried on a simple program, same thing happens:
==============================
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
std::ifstream loop_file(argv[1]);

if (!loop_file)
cout<<"cannot open file: "<<argv[1];
else
cout<<"successfully opened"<<endl;
}
==================
# ./tracer input.data
cannot open file input.data
But it works well on smaller input file, (e.g 200Mb)

I guess there is a limit of size on "std::ifstream" object.
I wonder if there is method I could open this large input file
in C++?

Any suggestion or pointer is highly appreciated!

Many thanks!
Charlie
Jul 23 '05 #1
3 6531
Charlie wrote:
Dear all,

I am currently writting a trace analyzer in C++.
It always fails to open a very large input file (3.7Gb).

I tried on a simple program, same thing happens:
==============================
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
std::ifstream loop_file(argv[1]);

if (!loop_file)
cout<<"cannot open file: "<<argv[1];
else
cout<<"successfully opened"<<endl;
}
==================
# ./tracer input.data
cannot open file input.data
But it works well on smaller input file, (e.g 200Mb)

I guess there is a limit of size on "std::ifstream" object.
I wonder if there is method I could open this large input file
in C++?


There is no limit imposed by C++, but on 32-bit machines you will need what
is called "large file support" in the operating system to open files larger
than 2^31 bytes. Linux definitely supports this and surely Windows does
too, but you will have to consult your compiler documentation as to how to
make this work for you. It is probably a compiler switch of some sort.

HTH,
Ian McCulloch

Jul 23 '05 #2
I don't know if this will fix your problem, but you could try to go
back to the older C style input/output routines fopen, fscanf, fprintf,
etc. If that works you could give it a try.

One thing to be aware of is that I've heard that mixing stdio and
iostream can lead to bad results, but I doubt it'd affect anythnig in
this case so you would probably be okay. But it's sorta playing with
fire, at least without knowing more about how much they interact.
Perhaps someone else can say something about that. It probably wouldn't
cause any problems (in fact I wouldn't be surprised if one is usually
implemented in terms of the other), but it might be wise to stay away
from iostream if you are using these functions. Perhaps Google could
say more.

Jul 23 '05 #3

Charlie wrote:
Dear all,

I am currently writting a trace analyzer in C++.
It always fails to open a very large input file (3.7Gb).

I tried on a simple program, same thing happens: (OK) I guess there is a limit of size on "std::ifstream" object.
I wonder if there is method I could open this large input file
in C++?


The problem isn't C++, but your particular implementation. A 64
bits implementation is unlikely to choke on it. A good 32 bits
implementation might also be able to handle it. Try MSVC 8, IIRC
it can handle these sizes. I do suspect that gcc can too, but I'm
less sure about that.

HTH,
Michiel Salters

Jul 23 '05 #4

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

Similar topics

15
by: solartimba | last post by:
Why can I open a file using a C-string but not a C++ string? //C-strings works char path; strcpy(path,"c:\test.txt"); ifstream infile(path); ...
3
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. ...
9
by: Charles F McDevitt | last post by:
I'm trying to upgrade some old code that used old iostreams. At one place in the code, I have a path/filename in a wchar_t string (unicode...
10
by: Gunnar G | last post by:
I'm having problem reading from the beginning of a file. Here is the code (more or less) ifstream codefin; ofstream codefout; while...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was...
2
by: Karl | last post by:
Hey everyone! I've got a quick question on whether std::ifstream is buffered or not. The reason is that I have a homework assignment that...
5
by: Assertor | last post by:
Hi, all. Is there any way to create an instance of std::ifstream using std::string. (through std::ifstream's constructor or assignment operator...
2
by: mpalomas | last post by:
Hi C++ folks, I have trouble to open files whose path contains non-ascii characters with std::ifstream. For instance let's say i just have a...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.