[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1844
  • Last Modified:

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

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
ormale67
Asked:
ormale67
1 Solution
 
kumvjuecCommented:
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
 
pratap_rCommented:
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
 
MattBeardCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now