Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-10-08
5
Medium Priority
?
208 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
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 375 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.
Suggested Courses

810 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