Files
WordsSortingCpp/WordsSorting/FileTools.cpp
2021-05-05 15:34:09 +02:00

40 lines
900 B
C++

#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();
}