我有一个包含 500,000 个随机(6-7 位)数字的文件。我想一次一个地将所有数字写入一个数组。Vector 将在此代码中完美运行,但恐怕我的老师不允许使用 Vectors。这是我的代码:
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();
当它尝试写入第 17 个元素时,我收到以下错误消息:“readfile.exe 中 0x60ad86f8 处的未处理异常:0xC0000005:访问冲突读取位置 0x003cb578。”
以下是前 18 个元素:
8809397
5937712
9169212
3467863
5730702
748737
6035700
577496
3601486
4490826
1749210
5058906
8252221
607331
5100676
1061913
3978612
2824658
有什么线索吗?