1

I've been scratching my head and putting this homework off for a couple days but now that I hunker down to try and do it I'm coming up empty. There's 4 things I need to do.

1) Read a binary file and place that data into arrays

2) Sort the list according to the test scores from lowest to highest

3) Average the scores and output it

4) Create a new binary file with the sorted data

This is what the binary data file looks unsorted

A. Smith 89

T. Phillip 95

S. Long 76

I can probably sort since I think I know how to use parallel arrays and index sorting to figure it out, but the reading of the binary file and placing that data into an array is confusing as hell to me as my book doesn't really explain very well.

So far this is my preliminary code which doesn't really do much:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;



int get_int(int default_value);
int average(int x, int y, int z);

int main() 

   {


    char filename[MAX_PATH + 1];
    int n = 0;
    char name[3];
    int grade[3];
    int recsize = sizeof(name) + sizeof(int);
    cout << "Enter directory and file name of the binary file you want to open: ";
    cin.getline(filename, MAX_PATH);    

    // Open file for binary write.
    fstream fbin(filename, ios::binary | ios::in);
    if (!fbin) {
    cout << "Could not open " << filename << endl;
    system("PAUSE");
    return -1;
   }

}

Sorry for such a novice question.

edit: Sorry what the data file stated earlier is what it SHOULD look like, the binary file is a .dat that has this in it when opened with notepad:

A.Smith ÌÌÌÌÌÌÌÌÌÌÌY T. Phillip ÌÌÌÌÌÌÌÌ_ S. Long ip ÌÌÌÌÌÌÌÌL J. White p ÌÌÌÌÌÌÌÌd

4

3 回答 3

2

在 C++ 中读取文件很简单:

从文件创建一个流[以便从流中读取](你有 filestream[input/output], stringstream ...)

    ifstream fin;  //creates a fileinput stream

    fin.open(fname.c_str(),ifstream::binary);        // this opens the file in binary mod  


    void readFile(string fname)
    {
        ifstream fin;
        fin.open(fname.c_str());            //opens that file;
        if(!fin)
            cout<<"err";
        string line;
        while(getline(fin,line))            //gets a line from stream and put it in line (string)
        {
            cout<<line<<endl;
            //reading every line
            //process for you need.
            ...
        }
        fin.close();
    }

正如您所指定的,该文件只是一个文本文件,因此您可以处理每一行并做任何您想做的事情。

于 2013-06-02T03:08:57.777 回答
1

从二进制文件中读取可能看起来很混乱,但实际上相对简单。您已经使用文件名声明了 fstream 并将其设置为二进制,这几乎没有什么可做的。

创建一个指向字符数组的指针(通常称为缓冲区,因为此数据通常是出于其他目的从该数组中提取的)。数组的大小由文件的长度决定,您可以使用以下方法获得:

fbin.seekg(0, fbin.end); //Tells fbin to seek to 0 entries from the end of the stream
int binaryLength = fbin.tellg(); //The position of the stream (i.e. its length) is stored in binaryLength
fbin.seekg(0, fbin.beg); //Returns fbin to the beginning of the stream

然后这用于创建一个简单的字符数组指针:

char* buffer = new char[binaryLength];

然后将数据读入缓冲区:

fbin.read(buffer, binaryLength);

文件中的所有二进制数据现在都在缓冲区中。这些数据可以像在普通数组中一样简单地访问,并且可以随心所欲地使用。

但是,您拥有的数据似乎根本不是二进制的。它看起来更像是一个普通的文本文件。也许,除非明确说明,否则您应该考虑使用不同的方法来读取数据。

于 2013-06-02T03:04:35.107 回答
1

您知道,使用低范围的排序索引,您可以避免实际排序(通过比较索引和来回移动数据)。您所要做的就是分配一个vectorof vector,将strings其大小调整为 101。然后遍历数据,将每个:“A. Smith”存储在第 89 个元素中;95-th的“T. Phillip”;76-th 中的“S. Long”等等。

begin()然后通过迭代从到的向量元素,end()所有数据都已经排序。

它几乎是线性的复杂性(几乎,因为子向量和字符串的分配/调整大小可能很昂贵)容易且透明。

于 2013-06-02T03:19:55.893 回答