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
Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …

856 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