I have a file with 500,000 random (6-7 digit) numbers. I want to write all the numbers, one at a time, to an array. A Vector will work flawlessly in this code BUT, I'm afraid my teacher just won't allow the use of Vectors. Here is my code:
int line_no = 0;
int num;
int* num_array = new int[];
//Open file for input
fstream in_file("CSCI3380_final_project_dataset.txt", ios::in);
//Test for file opening
if (!in_file)
{
cout << "Cannot open words1.txt for reading" << endl;
exit(-1);
}
//Read file
while(true)
{
//Read one line at a time
in_file >> num;
//Test for eof
if (in_file.eof())
break;
num_array[line_no] = num;
//Increment array position
line_no++;
}
//Close the file
in_file.close();
I get the following error message when it tries to write the 17th element: "Unhandled exception at 0x60ad86f8 in readfile.exe: 0xC0000005: Access violation reading location 0x003cb578."
Here are the first 18 elements:
8809397
5937712
9169212
3467863
5730702
748737
6035700
577496
3601486
4490826
1749210
5058906
8252221
607331
5100676
1061913
3978612
2824658
Any clues?