Solved

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

Posted on 2004-10-08
5
201 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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