Solved

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

Posted on 2004-10-08
5
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

726 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