x.Hmeen trying to do this program for quite a while now and it works but it is h
ID: 3617894 • Letter: X
Question
x.Hmeen trying to do this program for quite a while now and it works but it is huge and there has to be a better way to do it. I am taking a text file that has about 900,000 words and sorting it and then saving it to a file and to do so I must break down the file into small files and then merge them until it is all merged into one file. If anyone has some code or way to do this so its easier and not a ridiculous amount of code.Below is my code so far and I don't want to keep going. PLEASE HELP! WILL GIVE LIFESAVER!
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
using namespace std;
bool sCompare(string, string);
void merge(char*, char*, char*);
char *append_char ( const char *, const char );
void output(string[], ofstream&);
const int SIZE = 30000;
int main ()
{
string words[SIZE];
ofstream fout;
int count = 0;
int count2 = 0;
//char fileExt[5] = ".txt";
ifstream fin;
fin.open("bigFile.txt");
// test to see if file opened correctly
if(fin.fail())
{
cout << "Error opening file. ";
return 0;
}
while(!fin.eof())
{
for(int i = 0; i < SIZE; i++)
{
fin >> words[i];
}
sort(words, words + SIZE, sCompare);
if (count == 0)
{
fout.open("text_1_0.txt");
output(words, fout);
fout.close();
}
else if (count == 1)
{
fout.open("text_1_1.txt");
output(words, fout);
fout.close();
}
else if (count == 2)
{
fout.open("text_1_2.txt");
output(words, fout);
fout.close();
}
else if (count == 3)
{
fout.open("text_1_3.txt");
output(words, fout);
fout.close();
}
else if (count == 4)
{
fout.open("text_1_4.txt");
output(words, fout);
fout.close();
}
else if (count == 5)
{
fout.open("text_1_5.txt");
output(words, fout);
fout.close();
}
else if (count == 6)
{
fout.open("text_1_6.txt");
output(words, fout);
fout.close();
}
else if (count == 7)
{
fout.open("text_1_7.txt");
output(words, fout);
fout.close();
}
else if (count == 8)
{
fout.open("text_1_8.txt");
output(words, fout);
fout.close();
}
else if (count == 9)
{
fout.open("text_1_9.txt");
output(words, fout);
fout.close();
}
else if (count == 10)
{
fout.open("text_1_10.txt");
output(words, fout);
fout.close();
}
else if (count == 11)
{
fout.open("text_1_11.txt");
output(words, fout);
fout.close();
}
else if (count == 12)
{
fout.open("text_1_12.txt");
output(words, fout);
fout.close();
}
else if (count == 13)
{
fout.open("text_1_13.txt");
output(words, fout);
fout.close();
}
Explanation / Answer
You can replace the first 30 if else statements naming the partial sorted file:... etcto the following:Ans in a similar way the huge merge if else:... etc To the following:You might need to add an include to stdio at the top of your code:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.