Problem with binary files

2 binary files f1 and f2,which components are symbol strings.Components are in alphabetical order. Have to merge f1 and f2 into f3. F3 components have to be in alphabetical order either. Also there must be an option to print f1 and f2 content.

Thats how far i have done,but something doesnt work.Dont know where is mistake.

MAIN.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>
#include <string>
#include "File.h"
using namespace std;

int main() {
    string inp1, inp2, outp;
    cout << "Enter filename of 1st input file: ";
    cin >> inp1;
    cout << "Enter filename of 2nd input file: ";
    cin >> inp2;
    cout << "Enter filename of output file: ";
    cin >> outp;
   
    char *i1 = new char[inp1.length()];
    for (int i=0; i<inp1.length(); i++)
        i1[i]=inp1[i];
   
    char *i2 = new char[inp2.length()];
    for (int i=0; i<inp2.length(); i++)
        i2[i]=inp2[i];
   
    char *o = new char[outp.length()];
    for (int i=0; i<outp.length(); i++)
        o[i]=outp[i];
   
    genInp(i1);
    genInp(i2);
    merge2file(i1, i2, o);
    return 0;
}


FILE.H

1
2
3
4
int parse2string(char *, std::string *&);
int strCompare(std::string, std::string);
void merge2file(char *, char *, char *);
void genInp(char *);



FILE.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include "File.h"
using namespace std;
int parse2string(char *filename, string *&outp) {
    int wCnt=0, elem=0, strChar=0;
    ifstream inp(filename);
    inp.seekg(0, ios::end);
    istream::pos_type size = inp.tellg();
    char *c = new char[size];
    inp.seekg(0, ios::beg);
    inp.read(c, size);
    inp.close();

    for (int i=0; i<size; i++)
        if (c[i]=='\n')
            wCnt++;
    wCnt++;

    outp = new string[wCnt];
    for (int i=0; i<size; i++) {
        if (c[i]=='\r')
            i++;
        if (c[i]=='\n') {
            elem++;
            strChar=0;
        } else {
            outp[elem] += c[i];
            strChar++;
        }
    }
    return wCnt;
}

int strCompare(string a, string b) {
    for (int i=0; i<a.length(); i++)
        a[i]=toupper(a[i]);
    for (int i=0; i<b.length(); i++)
        b[i]=toupper(b[i]);
    if (a==b)
        return 0;
    if (a>b)
        return 1;
    if (a<b)
        return -1;
}
void merge2file(char *inpFile1, char *inpFile2, char *outpFile) {
    cout << "Merging files..." << endl;
    string *first, *second;
   
    int words1 = parse2string(inpFile1, first);
    int words2 = parse2string(inpFile2, second);
   
    ofstream outp(outpFile, ios::trunc);
    int pos1=0, pos2=0;
   
    while (pos1<words1 && pos2<words2) {
        switch (strCompare(first[pos1], second[pos2])) {
        case 1:
            outp << second[pos2] << endl;
            pos2++;
            break;
        case -1:
            outp << first[pos1] << endl;
            pos1++;
            break;
        case 0:
            outp << first[pos1] << endl;
            pos1++;
            pos2++;
            break;
        }
    }
    if (pos1==words1) {
        for (int i=pos2; i<words2; i++)
            outp << second[i] << endl;
    } else if (pos2==words2) {
        for (int i=pos1; i<words1; i++)
            outp << first[i] << endl;
    }
    delete [] first;
    delete [] second;
    outp.close();
}
void genInp(char * outpFile) {
    cout << "Generating inp file..." << endl;
    string * dict;
    int wCnt = parse2string("eng_dict.txt", dict);
    ofstream outp(outpFile, ios::trunc);
    for (int i=0; i<wCnt; i++)
        if(rand()%2)
            outp << dict[i] << endl;
    outp.close();
}
Last edited on
The sorting code you have will only work if then eng_dict.txt file is already sorted alphabetically.

If eng_dict is sorted, then the sorting looks like it would work.

The only other thing I noticed was that you aren't null terminating your char arrays:

1
2
3
    char *i1 = new char[inp1.length()];
    for (int i=0; i<inp1.length(); i++)
        i1[i]=inp1[i];


Remember that C-style strings (char arrays) need to end with a null character to indicate the string has ended.

But really... you have no reason to be doing this anyway. You can get the c string from a string with the c_str function:

1
2
3
4
5
6
7
8
9
10
    //  this is what you're doing.  This is wasteful and extra work for yourself:
    char *i1 = new char[inp1.length()];
    for (int i=0; i<inp1.length(); i++)
        i1[i]=inp1[i];
   
    genInp(i1);


    // this is what you should do instead:
    genInp( inp1.c_str() );



Of course you'll also have to change genInp (and all of your other functions) to take const char* instead of char*

There are other oddities in the code, but those are the only bugs that I spotted.
Last edited on
Topic archived. No new replies allowed.