在不使用数组的情况下对二进制文件中的数据进行排序 [英] Sorting data in binary file without using arrays

查看:142
本文介绍了在不使用数组的情况下对二进制文件中的数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个作业,应该在其中生成随机数.然后将它们写入二进制文件.之后,我应该读取数据,然后将其打印到屏幕上.最后,我必须对数据进行排序并输出.我必须不使用数组来做到这一点.我能够完成前两个部分.但是,我无法正确完成最后一部分,因此我去了google和youtube寻找有关如何执行此操作的说明,但是我很走运. 我真的很想知道我做错了什么,谢谢.

Hello I have an assignment where I am supposed to generate random numbers. Then write them to a binary file. After that I am supposed to read the data print it to the screen. Then finally, I have to sort the data and output. I have to do that without using arrays. I was able to do the first two parts. However, I couldn't do the last part correctly, so I went to google and youtube looking for an illustration on how to do that, but I was out of luck. I would really love to know what is it that I am doing wrong, thank you.

我认为我应该使用递归fseek和强制转换来避免使用数组.但是,我不知道如何使用它们.

I think I am supposed to use recursion fseek and casting to avoid using arrays; however, I don't know how to use them.

这是我的代码以及输出:

Here is my code along with the output:

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

using namespace std;

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

//**********************

int main()
{
    int num = 0;

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

    Write_File(num);
    Read_File(num);
    Compare(num);


    return 0;
}

//***************

// Functions Definitions
void Write_File(int num)
{
    ofstream fout("sortfile.dat", ios::out | ios::binary | ios::beg);
    int number = 0;

    if (fout.is_open())
    {
        for (int x = 0; x < num; x++)
        {
            number = rand();
            fout << number << endl;
        }

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

void Read_File(int num)
{
    ifstream fin("sortfile.dat", ios::in | ios::binary | ios::beg);
    int number = 0;

    cout << endl << "randomly generated numbers befor sorting\n";
    if (fin.is_open())//check first if open already
    {
        for (int i = 0; i < num; i++)
        {
            fin >> number;
            cout << number << endl;
        }


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


void Compare(int num) 
{
    int number = 0;
    int temp = 0;
    int hold = 0;
    ifstream fin("sortfile.dat", ios::in | ios::binary | ios::beg);

    if (fin.is_open())//check first if open already
    {
        for (int i = 0; i < num; i++)
        {
            fin >> number;
            fin >> temp;
            if (number > temp)
            {


                hold = temp;
                temp = number;
                number = hold;

            }
            else
            {
                ofstream fout("sortfile.dat", ios::out | ios::binary | ios::beg);
                fout << number;
                fout << temp;
            }

            ofstream fout("sortfile.dat", ios::out | ios::binary | ios::beg);
            fout << number;
            fout << temp;
        }
        cout << endl << "Nums after sort\n";
        for (int i = 0; i < num; i++)
        {
            fin >> number;
            cout << number << endl;
        }

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

输出:

Enter a positive number to generate random numbers 4

randomly generated numbers befor sorting
41
18467
6334
26500

Nums after sort
6334
6334
6334
6334
Press any key to continue . . .

推荐答案

好的,这里有些错误.首先,仅交换相邻元素就无法进行一次通过排序.那将是O(n)排序,并且如果您设法实现这一点,那么您将是即时的计算机科学名人.你的教授说你不能使用数组?如果说数组",他/她指的是任何类型的内存缓冲区,那真是愚蠢.从字面上看,如果您想要一种不使用数组的排序,则只需使用std :: map即可.从文件中读取数字时,将其插入地图.然后,使用迭代器按已排序的顺序检索数字,并按需打印出来.

Ok, there are several things wrong here. First, you are not going to be able to do a one pass sort by just swapping adjacent elements. That would be an O(n) sort, and if you managed to implement that, you would be an instant computer science celebrity. Your professor said you can't use arrays? If by "arrays", he/she meant any kind of in-memory buffer, then that's just silly. If you want a sort that doesn't use arrays, in a literal sense, just use a std::map. Insert the numbers into the map as you read them from the file. Then, use an iterator to retrieve the numbers in sorted order, printing them out as you do so.

第二,您正在使用文件io做一些可能不想要的事情.在循环中,您从文件中读取数字对,然后有可能交换它们,如果它们按排序顺序,则控制流程允许将这对数字写入文件两次.同样,在每次循环迭代中,您将构造一个或可能两个流对象.每个新的输出流都从文件的开头开始,因此您要重复写入文件的开头.在打印出已排序"的内容之前,您也不会在文件的开头查找输入流.之所以要打印4次相同的数字,是因为它已到达文件末尾,并且没有为数字"分配任何新值.

Secondly, you are doing some things with file io that are probably not what you want. In your loop where you read pairs of numbers from the file and then possibly swap them, your control flow allows for the pair of numbers to be written twice to the file, in the event that they are in sorted order. Also, you construct one, or possibly two, ofstream objects in each loop iteration. Each new output stream starts at the beginning of the file, so you're repeatedly writing to the beginning of the file. You also don't seek your input stream to the beginning of the file before printing out the "sorted" contents. The reason it's printing out the same number 4 times is that it's reached the end of the file, and isn't assigning any new value to "number".

我的建议是真正避免尝试同时读取和写入文件.阅读中的数字(请注意,不要数组),对它们进行排序,然后输出. (从分配规范看,您似乎不必再次将它们写到文件中,但我不确定).

My advice would be to really avoid trying to read and write to the file at the same time. Read the numbers in (not to an array, mind you ;) ), sort them, then output them. (From the assignment spec, it doesn't seem like you have to write them to the file again, but I'm not sure).

这篇关于在不使用数组的情况下对二进制文件中的数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆