0

Sup, guys. I really need some help with my code. So, i`ve created 2 binary files and I need to create a third file which contains data from 2 previous files in sorted manner (structure of a file is number(int) and text(char), and file needs to be sorted in ascending manner by number (the text must follow the number).

Here`s an example(don't mind the commas):

file1.txt:

1 text1, 7 text2, 4 text3

file2.txt:

3 text4, 8 text5, 2 text6

so the file 3 should be:

1 text1, 2 text6, 3 text4, 4 text3, 7 text2, 8 text5

So far i have, like I said, created 2 binary files which have a number and text written into them. I've also kinda written the content of files into an array. The issue is - I have no idea how to sort them and put them into file.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;


class faili {
int atslega;
char * komponente;

public:
faili(int x, char * s) {
    komponente = new char[20];
    strcpy(komponente, s);
    atslega = x;
}
faili() {
    komponente = new char[20];
    atslega = 0;
}

void writeToFile(fstream & f) {
    f.write((char *)&atslega, sizeof(int));
    f.write(komponente, 20);
}

void readFromFile(fstream & f) {
    f.read((char *)&atslega, sizeof(int));
    f.read(komponente, 20);
}

int getKey() {
    return atslega;
}

void printIt() {
    cout << atslega << " " << komponente << endl;
}
};

typedef vector<faili *> failiMas;
void printMas(failiMas arr) {
    for (failiMas::iterator it = arr.begin(); it != arr.end(); it++) {
        (*it)->printIt();
    }
};

class salidzinat {
public:
bool operator()(faili * i1, faili * i2) {
    return i1->getKey() > i2->getKey();

 }
 };


    typedef vector<faili *> failiMas;

    int main() {

        ofstream outputFile1w("f1.txt");

        /*faili * i1 = new faili(1, "sad");
        faili * i2 = new faili(3, "asdasd");
        faili * i3 = new faili(6, "dfgdfc");
        faili * i4 = new faili(4, "dija");
        faili * i5 = new faili(8, "yjk");
        faili * i6 = new faili(2, "ruifjer");
        */

        faili ier1(1, "sad");
        faili ier2(3, "asdasd");
        faili ier3(6, "dfgdfn");
        faili ier4(4, "dija");
        faili ier5(8, "yjk");
        faili ier6(2, "ruifjer");

        fstream f1("f1.txt");

        ier1.writeToFile(f1);
        ier2.writeToFile(f1);
        ier3.writeToFile(f1);

        f1.close();

        f1.open("f1.txt");

        faili a, b, c;

        a.readFromFile(f1);
        b.readFromFile(f1);
        c.readFromFile(f1);

        cout << "Faila f1.txt saturs: \n";

        a.printIt();
        b.printIt();
        c.printIt();

        f1.close();

        ofstream outputFile2w("f2.txt");

        fstream f2("f2.txt");

        ier4.writeToFile(f2);
        ier5.writeToFile(f2);
        ier6.writeToFile(f2);

        f2.close();

        f2.open("f2.txt");

        faili d, e, f;

        d.readFromFile(f2);
        e.readFromFile(f2);
        f.readFromFile(f2);

        cout << "Faila f2.txt saturs: \n";

        d.printIt();
        e.printIt();
        f.printIt();

        f2.close();

        ifstream inputFile1r;
        inputFile1r.open("f1.txt", ios::in);
        int count = 0;
        string textarr[5];
        string textsave = "Null";

        while (!inputFile1r.eof()) {
            inputFile1r >> textsave;
            textarr[count] = textsave;

            cout << textarr[count] << endl;
            count++;
        }
        inputFile1r.close();

        ifstream inputFile2r;
        inputFile2r.open("f2.txt", ios::in);;

        while (!inputFile2r.eof()) {
            inputFile2r >> textsave;
            textarr[count] = textsave;

            cout << textarr[count] << endl;
            count++;
        }
        system("Pause");

        //-----------------------------------------------------------------------------------------------------------------------

    };

I, of course, don't need you to do a homework for me :P. I just kinda want to know if I'm thinking correctly and what am i supposed to do next as I`m quite lost.

1 Answer 1

0

You can use eof() condition inside a loop and keep track of all the numbers. If a new number comes up that is greater than 'greatest', you move it ahead in the array. There are a number of sorting techniques you can use but this is the most basic one. Once you have the sorted array, you could simply write the data in third file.

#include <unistd.h>
int c;
int greatest = 0;
int arr = new int[N];

while ((c = getchar()) != EOF)
{
    faili l;
    string s = l.readFromFile(file);
    int num = atoi(s);

    #Compare with greatest and write in array

}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.