#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <cctype>
#include <ranges>
namespace rngs = std::ranges;
auto get_words(const std::string& fn) {
staticconstexprchar split { '-' };
std::ifstream ifs(fn);
std::vector<std::string> words;
for (std::string wrd; ifs >> wrd; ) {
std::string w;
rngs::copy_if(wrd, std::back_inserter(w), [](unsignedchar ch) {return ch == split || std::isalpha(ch); });
for (constauto& word : rngs::split_view(w, split))
if (word.begin() != word.end())
words.emplace_back(word.begin(), word.end());
}
return words;
}
int main() {
const std::string path_to_file { "test.txt" };
std::ofstream(path_to_file) << "'Twas brillig, and the slithy-toves !@#$%^&*\n""Did gyre! (and gimble) in the wabe:\n""All \"mimsy\" were the borogoves,\n""And the mome raths outgrabe.\n";
std::cout << "file contains:\n----------\n" << std::ifstream(path_to_file).rdbuf() << "\nwords:\n--------\n";
for (constauto& w : get_words(path_to_file))
std::cout << w << '\n';
}
file contains:
----------
'Twas brillig, and the slithy-toves !@#$%^&*
Did gyre! (and gimble) in the wabe:
All "mimsy" were the borogoves,
And the mome raths outgrabe.
words:
--------
Twas
brillig
and
the
slithy
toves
Did
gyre
and
gimble
in
the
wabe
All
mimsy
were
the
borogoves
And
the
mome
raths
outgrabe