Sorting a file

Write your question here.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  #include<iostream>
#include<fstream>
#include<string>

using namespace std;

void Split(fstream &inFile, fstream &auxFileOne,fstream &auxFileTwo,int partLength)
{
    string buffer;
    int counter;
    do
    {
        counter = 1;
        while((counter <= partLength) && !inFile.eof())
        {
            getline(inFile,buffer);
            auxFileOne<<buffer;
            counter++;
        }
        counter = 1;
        while((counter <= partLength) && !inFile.eof())
        {
            getline(inFile,buffer);
            auxFileTwo<<buffer;
            counter++;
        }
    }
    while(!inFile.eof());
}

void Combine(fstream &auxFileOne,fstream &auxFileTwo,fstream &inputFile,int partLength)
{
    string bufferOne;
    string bufferTwo;
    int counterOne;
    int counterTwo;
    if(!auxFileOne.eof())
        getline(auxFileOne,bufferOne);
    if(!auxFileTwo.eof())
        getline(auxFileTwo,bufferTwo);
    while(!auxFileOne.eof() && !auxFileTwo.eof())
    {
        counterOne = 1;
        counterTwo = 1;
        do
        {
           if(bufferOne < bufferTwo)
           {
                inputFile << bufferOne;
                if(!auxFileOne.eof())
                    getline(auxFileOne,bufferOne);
                counterOne++;
           }
           else
           {
               inputFile<<bufferTwo;
               if(!auxFileTwo.eof())
                    getline(auxFileTwo,bufferTwo);
               counterTwo++;
           }
        }
        while(counterOne <= partLength && counterTwo <= partLength && !auxFileOne.eof() && !auxFileTwo.eof());
        while(counterOne <= partLength && !auxFileOne.eof())
        {
            getline(auxFileOne,bufferOne);
            inputFile << bufferOne;
            counterOne++;
        }
        while(counterTwo <= partLength && !auxFileTwo.eof())
        {
            getline(auxFileTwo,bufferTwo);
            inputFile<<bufferTwo;
            counterTwo++;
        }
    }
    while(!auxFileOne.eof())
    {
        getline(auxFileOne,bufferOne);
        inputFile<<bufferOne;
    }
    while(!auxFileTwo.eof())
    {
        getline(auxFileTwo,bufferTwo);
        inputFile<<bufferTwo;
    }
}

int main()
{
    string pathA,pathB,pathC;
    fstream inputFile,auxFileOne,auxFileTwo;
    int partLength = 1;
    bool exitCond = false;
    cout<<"Enter the path to the file" << endl;
    getline(cin,pathA);
    pathB=pathA.substr(0,pathA.find("."))+"_tmp001.txt";
    pathC=pathA.substr(0,pathA.find("."))+"_tmp002.txt";
    do
    {
        inputFile.open(pathA.c_str(),ios::in);
        auxFileOne.open(pathB.c_str(),ios::out);
        auxFileTwo.open(pathC.c_str(),ios::out);

        Split(inputFile,auxFileOne,auxFileTwo,partLength);

        auxFileOne.close();
        auxFileTwo.close();
        inputFile.close();

        inputFile.open(pathA.c_str(),ios::out);
        auxFileOne.open(pathB.c_str(),ios::in);
        auxFileTwo.open(pathC.c_str(),ios::in);

        if(!auxFileTwo.eof())
        {
            Combine(auxFileOne,auxFileTwo,inputFile,partLength);
            partLength*=2;
        }
        else
            exitCond = true;

        auxFileTwo.close();
        auxFileOne.close();
        inputFile.close();
    }
    while(!exitCond);
    return 0;
}


What is wrong in this code ?

Why don't you tell us ?
Do you have compile errors, crashes, wrong output.....?
what is the sence of the loop in line 62 ?
IgorMVK wrote:
what is the sence of the loop in line 62 ?


That is part of the do loop starting on line 45. For the OP, it's a good idea to place the while part on the same line as the closing brace.

@nowy20180820

It's not a good idea to loop on eof, getline copes with eof quite happily.

In my personal opinion, the fact that a do loop always executes at least once is not a sole reason for selecting that type of loop. Kernighan & Ritchie (the inventors of the C language ) were not a fan of them. One thing they did mention was using it when something must be done regardless, the example being writing the null at the end of a C string, even if it is otherwise empty. None of your loops meet that criteria.

Edit: It's confusing that you write to the Input File. Where is the sorting?
Last edited on
What is wrong in this code ?
The biggest problem is that you posted it, and judging by this question and past posts, you're kind of an idiot. Learn to ask questions the right way or get lost.
Topic archived. No new replies allowed.