Solved

How to open and read a huge file in C++

Posted on 2004-10-11
4
1,794 Views
Last Modified: 2008-01-09
I wrote the fallowing code, but there is an error when I try to read the information,  Could some person help me!

#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS_64

#include <windows.h>
#include <iostream.h>
#include <fstream.h>

void main ()
{


 char *path1 = "C:\\Reconstruction\\quad-HIDAC_data\\Huge_File.lst";
 LPVOID     buffer = 0;
 DWORD      dwRead;
 DWORD      error;

  int event;

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

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

     error = GetLastError(); // This is the error: 998 Invalid access to memory location.  ERROR_NOACCESS

  // ...
 }

  CloseHandle ( hFile);

}
0
Comment
Question by:ormale67
4 Comments
 
LVL 3

Expert Comment

by:kumvjuec
ID: 12278791
hi ormale67,

What is the size of your file? And what is the RAM you are having?
This error is possibly arising because you are trying to read too much data and your RAM is not that big.
0
 
LVL 5

Accepted Solution

by:
rsriprac earned 500 total points
ID: 12278818
0
 
LVL 11

Expert Comment

by:pratap_r
ID: 12279079
umm.. you will have to allocate the buffer before you use it...!!! or was that just a typo!!?

use malloc or globalalloc to allocate the memory first.... and CreateFileMapping & MapFileToView for larger files..

Pratap
0
 

Expert Comment

by:MattBeard
ID: 12310607
Your call:

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

Will read the first 1024 bytes from the file and write them to the memory location specified by the void pointer "buffer".

As buffer is set to 0 this will try to write the data to the start of memory - which is a bad thing to try and do and so it will fail.

You need to allocate a buffer at least 1024 bytes in size and read the data into that.

Which way you allocate the buffer will depend on the way that the rest of your program handles memory allocation - below is an example using "new".

void main ()
{
 char *path1 = "C:\\Reconstruction\\quad-HIDAC_data\\Huge_File.lst";
 unsigned char *buffer = new unsigned char[1024];
 DWORD      dwRead;
 DWORD      error;

  int event;

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

     event = ReadFile ( hFile, (LPVOID)buffer, 1024, &dwRead, NULL);

     error = GetLastError(); // This is the error: 998 Invalid access to memory location.  ERROR_NOACCESS

  // ...
 }

  delete[] buffer;      // Don't forget to free up any memory you allocate!

  CloseHandle ( hFile);
}
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

17 Experts available now in Live!

Get 1:1 Help Now