【发布时间】:2015-12-15 05:15:34
【问题描述】:
对于我的编程项目,我应该编写一个对磁盘上的整数进行排序的程序(即离线排序)。我首先应该生成一些随机整数,将它们全部写入,然后读取其中两个整数进行交换,然后将它们写回磁盘,然后重复这些步骤,直到对数字进行排序。我能够很好地生成随机数,并且打开文件没有问题,但是当尝试写入文件时它会崩溃。这是我用来实现排序的程序的代码片段,其中包含排序算法,以及使我的程序崩溃的代码片段:
void ReadAndWrite(int & rand_ints, int & pos_ints)
{
int rand_ints2 = 0;
GetNumber(pos_ints);
srand(time(0));
fstream finout;
finout.open(SORTFILE, ios::binary | ios::in | ios::out);
if (finout.is_open())
{
for (int i = 0; i < pos_ints; ++i)
{
rand_ints = rand() % 5;
finout.write(reinterpret_cast <char *>(rand_ints), sizeof(int) * 1);
}
for (int i = 0; i < pos_ints; ++i)
{
finout.seekg(ios::beg);
finout.read(reinterpret_cast <char *>(rand_ints), sizeof(int) * 2);
bubbleSort(&rand_ints, 2, sizeof(int), compare_ints);
finout.seekp(ios::app);
finout.write(reinterpret_cast <char *>(rand_ints), sizeof(int) * 2);
}
finout.close();
}
else
{
cout << "File not opened!" << endl;
}
}
void GetNumber(int & pos_ints)
{
cout << "Enter a positive number: ";
cin >> pos_ints;
}
void bubbleSort(void * base, size_t num, size_t width, int(*compar) (const void *, const void *))
{
bool done = false;//Allows us to enter loop first time
int hi = num - 1;//largest index is 1 less than the size of the array
while (!done)
{
done = true;//assume the list is sorted
for (int i = 0; i<hi; i++)
{
//pass thru the array up to 'hi'
//if (arr[i+1]<arr[i])
if (compar((void *)(((char *)base) + width*(i + 1)), (void *)(((char *)base) + width*(i))) < 0)
{
//if any pair are out of order
done = false;//the list is not sorted
//int temp = arr[i];//swap them
void * tempp = (void *) new char[width];
memcpy_s(tempp, width, (((char *)base) + width*(i)), width);
//arr[i] = arr[i+1];
memcpy_s((((char *)base) + width*(i)), width, ((char *)base) + width*(i + 1), width);
//arr[i+1]=temp;
memcpy_s(((char *)base) + width*(i + 1), width, tempp, width);
delete[] tempp;
}
}
hi--;//at the end of a pass, largest item in that pass is in proper place; no need to go this far next time
}
}
int compare_ints(const void * arg1, const void * arg2)
{
int return_value = 0;
if (*(int *)arg1 < *(int *)arg2)
return_value = -1;
else if (*(int *)arg1 > *(int *)arg2)
return_value = 1;
return return_value;
}
它在代码 finout.write(reinterpret_cast (rand_ints), sizeof(int) * 1);在第一个 for 循环(第 52 行)中,出现以下错误:ExternalSort.exe 中的 0x55916D16 (msvcp140d.dll) 引发异常:0xC0000005:访问冲突读取位置 0x00000001。 有没有办法修复这个错误并使这个排序程序工作?尝试了我可能尝试的所有方法,但我看不到导致程序崩溃或导致其他问题的代码行。
【问题讨论】: