0

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?

cerealspiller
  • 1,401
  • 3
  • 14
  • 23
  • `std::vector` isn't allowed? How suck... – ikh Apr 12 '14 at 04:28
  • I suggest doing `if ( !(in_file >> num) ) break;`. Otherwise your code goes into an infinite loop if the read fails for any reason other than end-of-file. – M.M Apr 12 '14 at 05:04
  • https://ideone.com/OerlSE is an example.. There are many others like it on SO. Just search for "reading file into array". – Brandon Apr 12 '14 at 05:33

2 Answers2

0

Read on web about serialization. You dont need to use vector but you can create your own serializable class. Which is not disallowed as I predict. So serialisation is a mechanism which simply saves binary info of a class to a file which allowes you to deserialize it back to memory by a bit of code.

Alexander.Iljushkin
  • 4,519
  • 7
  • 29
  • 46
0

This is a syntax error:

int* num_array = new int[];

You have to specify a number inside the brackets. Based on your description , perhaps you want:

int* num_array = new int[500000];

Of course, your loop should also check you do not exceed whatever bound you choose here.

If your compiler does not complain about that line, it must be some bizarre compiler extension. Try invoking your compiler in standards-conforming mode (e.g. for g++ use -std=c++98 or -std=c++11, and -pedantic).

If you want to allocate a smaller array and later grow it if the file turns out to be long; then you will have to specifically do all that, i.e. allocate the bigger array when you reach a certain count, and copy over all the values, and delete the old array.

M.M
  • 138,810
  • 21
  • 208
  • 365