#include<string> // std::string
#include<vector> // std::vector<>
#include<iostream> // std::cout | std::cin | std::cerr
#include<fstream> // std::ifstream | std::ofstream
#include<cstdlib> // std::exit()
#include<algorithm> // std:: sort()
#include <locale> // std::tolower | st::locale
#include <cctype> //tolower
using namespace std;
vector<string> load_vector(const string&);
void save_vector(const vector<string>&, const string&);
void error(const string&);
int main() {
string filename = "data.txt";
vector<string> text = load_vector(filename);
string filename2 = "data2.txt";
fstream file; // start to see if we want to overwrite the file if the file already exist
file.open("data2.txt", ios_base::out | ios_base::in); // will not create file
if (file.is_open())
{
cout << "Warning, file already exists, proceed?";
if (!file.is_open())
{
file.close();
}
}
else
{
file.clear();
file.open("data2.txt", ios_base::out); // will create if necessary
}
sort(text.begin(), text.end());
text.erase(unique(text.begin(), text.end()), text.end());
/*for (unsigned int i=0; i<text.size(); i++) {
text[i] = tolower ();
}
// for (auto word : text) {
// string lowcase;
// for (auto ch : word)
// if (ch>='A' and ch<='Z') lowcase += ch +'a'-'A' ; else lowcase = ch;
// word = lowcase;
} */
save_vector(text, filename2);
return 0;
}
vector<string> load_vector(const string& filename) {
vector<string> vi;
ifstream ifile {filename};
if (!ifile)
error("load_vector cannot open the file " + filename);
copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
return vi;
}
void save_vector(const vector<string>& vi, const string& filename2) {
ofstream ofile ("data2.txt", ofstream::out);
if (!ofile)
error("save_vector cannot open the file " + filename2);
for (auto n : vi)
ofile << n << ' ';
ofile.close();
cout << "\nVector written in file \"" << filename2 << "\"" << endl;
}
/* return true if two vector<string> contain exactly the same members
bool compare(const vector<string>& v1, const vector<string>& v2) {
if (v1.size()!=v2.size())
return false; // vectors have different sizes
for (int i=0; i<v1.size(); ++i)
if (v1[i]!=v2[i])
return false; // i-th members of v1 and v2 are different
return true;
}
*/
void error(const string& s) {
cerr << "Error: " << s << endl;
exit(EXIT_FAILURE);
}