Solved

open a very huge file (48 GB) in C++ or C

Posted on 2004-10-08
5
199 Views
Last Modified: 2012-08-13
I need open a very huge file and until now I could not open it!
So I need help about it.    The characteristics of my PC are: 2GB RAM, 2.99 GHz, I am compiling with Visual C++ and OS is Windowd

main()
{

  char *path1 = "C:\\Reconstruction\\quad-HIDAC_data\\Test_File.lst";
  char buffer[1024];

  fstream fin;   // object from fstream class

  fin.open(path1,ios::binary,ios::in);
 
  if(!fin.is_open())
  {
        cout << "Fail....";
        return 0;
  }
  else
  {
 
    // do somthig


     fin.close();
  }

}
0
Comment
Question by:ormale67
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 12261416
A file of that size will fail because of the 32bit size limit. You'll have to use the Win32 API to access it, e.g.

#include <windows.h>

void main () {


 char *path1 = "C:\\Reconstruction\\quad-HIDAC_data\\Test_File.lst";
 char buffer[1024];
 DWORD dwRead;

 HANDLE  hFile = CreateFile ( path1, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

 while ( 1) {

  ReadFile ( hFile, buffer, 1024, &dwRead, NULL);

  // ...
 }

 CloseHandle ( hFile);
}
0
 

Author Comment

by:ormale67
ID: 12261460


I will complie this code,

thanks !

0
 
LVL 30

Expert Comment

by:Axter
ID: 12262338
I recommend that you also use MapFile API function in addition to CreateFile API function.

That will speed up your code when reading and writing.
0
 

Author Comment

by:ormale67
ID: 12652949
I already answer this question, and this is the code that could work for open a huge file:  This example was found
with the help of Visual C++ 6.0.

/* OPEN.C: This program uses _open to open a file
 * named OPEN.C for input and a file named OPEN.OUT
 * for output. The files are then closed.
 */

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>

void main( void )
{
   int fh1, fh2;

   fh1 = _open( "OPEN.C", _O_RDONLY );
   if( fh1 == -1 )
      perror( "open failed on input file" );
   else
   {
      printf( "open succeeded on input file\n" );
      _close( fh1 );
   }

   fh2 = _open( "OPEN.OUT", _O_WRONLY | _O_CREAT, _S_IREAD |
                            _S_IWRITE );
   if( fh2 == -1 )
      perror( "Open failed on output file" );
   else
   {
      printf( "Open succeeded on output file\n" );
      _close( fh2 );
   }
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now