2

I have a (binary) file which has multiple entries of an array of 6 elements. So the file would be structured something like this :

{1 2 12 18 22 0} {11 17 20 19 20 7} {3 9 18 24 0 9}...

where I have put brackets around the elements that form one array. I'd like to sort the file based only on the first element of each array, so the sorted file should read

{1 2 12 18 22 0} {3 9 18 24 0 9} {11 17 20 19 20 7}...

How would I go about doing this?

2
  • 1
    Do you know how each element is serialized (i.e. represented as a sequence of bytes)? For instance, is each element no larger than 255 and stored as a single byte (i.e. the total file size is evenly divisible by 6)? Commented Dec 13, 2013 at 14:52
  • @FrerichRaabe - Each element is a 4 byte integer. Commented Dec 14, 2013 at 11:35

2 Answers 2

2
  1. Read file into 2 dimensional array. Each element on the first dimension should hold six elements.
  2. Implement comparison function for qsort.
  3. Use qsort with your comparision function to sort the array.
  4. Write array back to file.
Sign up to request clarification or add additional context in comments.

Comments

-1
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

int main () {

    vector < vector<int> > v;
    vector <int> t;

    t.push_back(4);
    t.push_back(5);
    t.push_back(6);
    v.push_back(t); 
    t.clear();
    t.push_back(1);
    t.push_back(2);
    t.push_back(3);
    v.push_back(t);

    sort(v.begin(),v.end());

    for (int i = 0; i < v.size(); i++){
        for (int j = 0; j < v[i].size(); j++){
             cout << v[i][j] << " ";
        }
        cout << endl;
    }



    return 0;

}

1 Comment

The question is tagged C; this answer is written in C++. It shows the disparity between the languages, but doesn't answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.