Initial commit for WordsSorting
This commit is contained in:
39
WordsSorting/FileTools.cpp
Normal file
39
WordsSorting/FileTools.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "FileTools.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
void FileTools::ExtractWordsFromFile(const string& iFilePath, vector<string>& oWords)
|
||||
{
|
||||
ifstream wordList;
|
||||
wordList.open(iFilePath);
|
||||
if (wordList.is_open()) {
|
||||
string word;
|
||||
while (getline(wordList, word)) {
|
||||
// TODO basic checks on the words to validate/sanitise the input
|
||||
if (!word.empty()) {
|
||||
oWords.push_back(word);
|
||||
}
|
||||
}
|
||||
wordList.close();
|
||||
}
|
||||
}
|
||||
|
||||
void FileTools::SaveRseult(const vector<string>& iWords, const string& iPath, bool isReverse)
|
||||
{
|
||||
fstream fileOut;
|
||||
fileOut.open(iPath, ios_base::out);
|
||||
if (fileOut.is_open()) {
|
||||
if (isReverse) {
|
||||
for (auto wordsIt = iWords.rbegin(); wordsIt != iWords.rend(); ++wordsIt) {
|
||||
fileOut << *wordsIt << "\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (auto wordsIt = iWords.begin(); wordsIt != iWords.end(); ++wordsIt) {
|
||||
fileOut << *wordsIt << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
fileOut.close();
|
||||
}
|
||||
Reference in New Issue
Block a user