I have question about string array!!

Hi, I have question about string array!!
I have a project from my algorithm class which is to implement sorting program that sorts large text file!! The sorting method is tournament sort. The program sorts portion of data and write into text file called runs. After all runs are made, the program merge them into one complete text file. I made a merge function as follows!

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
void merge(ifstream& infile1, ifstream& infile2, ofstream& outfile, const size_t& bufsize, const string& in_title1, const string& in_title2, const string& out_title){
    infile1.open(in_title1.c_str(), ios::in);
    infile2.open(in_title2.c_str(), ios::in);
    outfile.open(out_title.c_str(), ios::out);
    string word1[bufsize];
    string word2[bufsize];
    string temp1, temp2; //to avoid repeating the last words...
    int index1 = 0, index2 = 0, w1_count = 0, w2_count = 0;
    int a,b,ele = 0;
    while(true){
fill_1:
        if(infile1.eof() || w1_count > 0) goto fill_2;
        index1 = 0;
        while(w1_count < bufsize && infile1 >> word1[w1_count]){
            w1_count++;
        }
fill_2:
        if(infile2.eof() || w2_count > 0) goto skip;
        index2 = 0;
        while(w2_count < bufsize && infile2 >> word2[w2_count]){
            w2_count++;
        }
skip:
        while(w1_count > 0 && w2_count > 0){
            if(word1[index1] < word2[index2]){
                outfile << word1[index1] + " ";
                index1++;
                w1_count--;
                ele++;
            }
            else{
                outfile << word2[index2] + " ";
                index2++;
                w2_count--;
                ele++;
            }
        }
        if(!infile1.eof() && !infile2.eof()){
            goto fill_1;
        }

        if(infile1.eof() && infile2.eof()){
            if(w1_count == 0 && w2_count > 0){
                while(w2_count >0){
                    outfile << word2[index2] + " ";
                    index2++;
                    w2_count--;
                    ele++;
                }
            }
            else if(w1_count > 0 && w2_count == 0){
                while(w1_count > 0){
                    outfile << word1[index1] + " ";
                    index1++;
                    w1_count--;
                    ele++;
                }
            }
            infile1.close();
            infile2.close();
            outfile.close();
            cout<< ele << " words are merged, and ";
            return;
        }
    }
}


The portion of code in main() that uses this function is as follows!!

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
    count = 3;
    int merge_count = 1;
    first = true; //reuse bool variable.
    string in_title1;
    string in_title2;
    string out_title;
    ostringstream t1, t2;
    ifstream infile1, infile2;
    while(count <= run_num){
        if(first){
            in_title1 = "run1.txt";
            in_title2 = "run2.txt";
            out_title = "merge1.txt";
            merge(infile1, infile2, outfile, bufsize, in_title1, in_title2, out_title);
            first = false;
            cout<<out_title<<" is created"<<endl;
        }
        else{
            t1 << count;
            t2 << merge_count;
            in_title1 = "run" + t1.str() + ".txt";
            in_title2 = "merge" + t2.str() + ".txt";
            merge_count++;
            t2.str("");
            t2 << merge_count;
            if(count == run_num){
                out_title = output_title;
            }
            else{
                out_title = "merge" + t2.str() + ".txt";
            }
            merge(infile1, infile2, outfile, bufsize, in_title1, in_title2, out_title);
            count++;
            t1.str("");
            t2.str("");
            cout<<out_title<<" is created"<<endl;

        }
    }


There will be several runs each has portion of the words from large text file in sorted order.. These two parts first takes two runs and merge them. And then it takes 3rd run and merge it with the first merged text file!!
I tested this program and it seems work with small text file!! But I generated random text file with size 10mb and pass bufsize = 32768, it stops after 3rd merge!! Anyone knows what wrong with this code??
Last edited on
This is not a good use case for goto.

The following:
1
2
    string word1[bufsize];
    string word2[bufsize];
is not legal C++. The size of an array must be a compile time constant. You probably have a compiler extension enabled that allows it - consider disabling it. Also, consider using a vector or two instead. If you're merging sorted files, there is no reason to use an array or container at all.

Don't loop on eof.
http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong

I suspect the largest problem is that you are reusing stream objects without resetting error/eof states. One way you could see if that is a problem is by checking to see if the streams were opened after you attempted to open them.
Hi thanks for your comment!!!
you are saying that the stream object can't be reused??
I thought it was ok if I make sure to close the stream after created a text file!!
Isn't is resetting everything after the stream object is closed??
I have another problem in my program that is sometimes the output text file has weird text in the file. But sometimes it worked fine!!
Topic archived. No new replies allowed.