二进制文件脱机排序失败

时间:2016-02-15 19:36:44

标签: c++ sorting binary

我应该编写一个程序,将随机生成的数字写入二进制文件,然后输出它们。之后,我必须将数字从最小到最大排序,并将它们显示在屏幕上。根据我的知识,一切正常,直到数据显示在排序之后。显示时数据没有排序,我无法理解。

这是我的代码,后跟输出:

GameObject

输出:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h> 


using namespace std;


//Function prototypes
void Write_File(int num);
void Sort(int num);
void Read_File(int num);


/******************************************************************************
* Assignment: Offline Sorting
*
* Overview:
*   This program is supposed to generate random numbers display them then 
*   save them to a binary file. Then reads the valuse sort them then dispaly 
*   the sorted data
*
* Input:
*   The user inputs how many random numbers should be generated
*
* Output:
*   
*   
*********************************************************************************/
int main()
{
    int num = 0;

    cout << "Enter a positive number to generate random numbers ";
    cin >> num;

    Write_File(num);
    Sort(num);
    Read_File(num);


    return 0;
}

 //Functions Definitions
/********************************************************************************
* Purpose:
*       This function write's the randomly generated numbers to a binary file
*
*********************************************************************************/
void Write_File(int num)
{
    ofstream fout("sortfile.dat", ios::out | ios::binary | ios::beg);
    int random = 0;

    //checks if file is opened
    if (fout.is_open())
    {
        //writes data to the file
        for (int x = 0; x < num; x++)
        {

            random = rand();
            fout.write(reinterpret_cast <char*> (&random), sizeof(int));
            cout << random << endl;
        }

        fout.close();
    }
    else
    {
        cout << "Error, File not opened" << endl;
    }
}

/********************************************************************************
* Purpose:
*       This function read's the form the binary file
*
*********************************************************************************/
void Read_File(int num)
{
    ifstream fin("sortfile.dat", ios::in | ios::binary | ios::beg);
    int number = 0;

    cout << endl << "randomly generated numbers after sorting\n";

    //checks if file is opened
    if (fin.is_open())
    {
        //reads data from the binary file
        for (int i = 0; i < num; i++)
        {
            fin.read(reinterpret_cast <char*> (&number), sizeof(int));
            cout << number << endl;
        }


            fin.close();
        }
        else
            cout << "File not opened in read phase." << endl;
}

/********************************************************************************
* Purpose:
*       This function compare's and sort's the numbers in the binary file
*
*********************************************************************************/
void Sort(int num)
{
    int num1 = 0;
    int num2 = 0;
    int temp = 0;

    fstream file_io("sortfile.dat", ios::out | ios::in | ios::binary | ios::beg);

    if(file_io.is_open())
    {
        for (int i = 0; i < num; i++)
        {
            for (int j = 0; j < num - i -1 ; j++)
            {
                file_io.seekg(sizeof(num1) * j);
                file_io.read(reinterpret_cast <char*> (&num1), sizeof(num1));
                file_io.seekg(sizeof(num2) * j - 1);
                file_io.read(reinterpret_cast <char*> (&num2), sizeof(num2));

                if (num1 > num2)
                {
                    temp = num1;
                    num1 = num2;
                    num2 = temp;
                }

                file_io.seekp(sizeof(num1) * j);
                file_io.write(reinterpret_cast <char*> (&num1), sizeof(num1));
                file_io.seekp(sizeof(num2) * j - 1);
                file_io.write(reinterpret_cast <char*> (&num2), sizeof(num2));
            }
        }
        file_io.close();

    }
}

1 个答案:

答案 0 :(得分:1)

Sort函数中,您正在寻找字节边界,而不是int边界(通常为4个字节)。而不是

file_io.seekg(sizeof(num2) * j - 1)

DO

file_io.seekg(sizeof(num2) * (j + 1) );

(你可以完成* (j-1),但如果第一个小于第二个,你就交换,所以你需要num2作为下一个数字,而不是前一个数字。)。

如果没有更改,我优化了对文件的写入,但除了修复该索引之外,没有任何变化:

void Sort(int num)
{
    int num1 = 0;
    int num2 = 0;
    int temp = 0;

    fstream file_io("sortfile.dat", ios::out | ios::in | ios::binary | ios::beg);

    if(file_io.is_open())
    {
        for (int i = 0; i < num; i++)
        {
            for (int j = 0; j < num - i -1 ; j++)
            {
                file_io.seekg(sizeof(num1) * j);
                file_io.read(reinterpret_cast <char*> (&num1), sizeof(num1));
                file_io.seekg(sizeof(num2) * (j + 1 ));
                file_io.read(reinterpret_cast <char*> (&num2), sizeof(num2));

                if (num1 > num2)
                {
                    file_io.seekp(sizeof(num1) * j);
                    file_io.write(reinterpret_cast <char*> (&num2), sizeof(num2));
                    file_io.seekp(sizeof(num2) * (j + 1));
                    file_io.write(reinterpret_cast <char*> (&num1), sizeof(num1));
                }
            }
        }
        file_io.close();

    }
}

注意:

  • 由于奇怪的警告,我不得不使用-fpermissive进行编译
  • 读取和写入这样的文件非常慢。