C++将一个二进制文件中的数据进行排序并保存到另一个文件的实现想法
思路:
先将源文件数据copy一份到目标文件,然后在目标文件中进行数据的排序。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
//写随机数到源文件,保证源文件存在并且存在数据。
void rand_write() {
cout << "This is from rand_write"<<endl;
ofstream in_file("d:\\f1.txt", ios::out);
srand((int)time(0));
A:
int n = rand() % (20-5)+5;
if (n == 0) goto A;
cout << "num:" << n << endl;
for (int i = 0; i < n; i++)
{
int temp = rand() % 100;
in_file.write((char*)(&temp), sizeof(int));
cout << temp <<