Reading large.txt file (8192x8192) using fstream -- stack overflow

Hello,

This is my first everpost here, as i am bit new to C++ and stuck in the code.
I am trying to read big .txt file which is giving erors. Firstly the code is running perfect and can read first 250,000 values but i cant read more then that. Basically after reading i am storing these values in 1D aray as you can see in the code..
CAn anybody provide me some expamle code to read big files such as 8192x8192. Or please help me what should I do to reasolve this issue in the following code ?
Thnaks


// Taken from http://www.cplusplus.com/doc/tutorial/files/

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

const unsigned int num_pixels = 250000; // instead i want to use
8192x8192 but has errors like stack overflow

unsigned long int Pixels[num_pixels];
long int count;
unsigned long int numberofpixels;

ifstream inputFile;

inputFile.open("friction_table.txt");

////////// Error Print if files not found
if (!inputFile)
{
cout << " Error Finding Input File" << endl;
system("pause");
exit(-1);
}


/////////reading input file
count = 0;
//while (!inputFile.eof())
while (count!= 250000)
{
inputFile >> Pixels[count];
count++;
}
numberofpixels = count;

///// Display whole file
for (count = 0; count < numberofpixels; count++)
{
cout << "" << Pixels[count];

}

///////// Display from specific location of array
cout << endl;
cout << count;
cout << endl;
cout << Pixels [4];
cout << endl;

system("pause");
return 0;


}
1. Please use [code][/code] tags when posting code.

2. unsigned long int Pixels[num_pixels];
Your average desktop OS usually provides between 1MB and 8MB of stack space per process.
8Kx8K is 67,108,864 elements, times whatever the size of your data type is.
If you want more, then allocate from the heap.

A cheap way is to do
unsigned long int *Pixels = new unsigned long[num_pixels];

A better way is to use an appropriate C++ container like std::vector (or std::array), then you don't have to worry about manual memory management.
you can increase the stack size on your program in the compiler settings. 60MB is rather small in memory terms but large for a stack -- computers have 8+ gigs now -- but I don't know what the upper limits on stack size are if you tried to do it this way. (As said above, the defaults are fairly small).

I wouldn't go this route in THIS case, but there are times when you have just a tiny bit more than the default allowed and you can fix this kind of error with at the compiler when that happens.

Topic archived. No new replies allowed.