Solved

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

Posted on 2004-10-11
4
1,829 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
[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
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

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 61
Best laptop programming computer for Windows development 14 82
Code planning methods/tools? 5 51
junit initializtion error 2 17
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
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 …

734 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