Solved

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

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

705 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