Jun 25, 2022 at 4:31pm Jun 25, 2022 at 4:31pm UTC
... and the OP disappears in a puff of smoke...
Jun 25, 2022 at 4:46pm Jun 25, 2022 at 4:46pm UTC
I'd hazard a guess the Wizards of The Lounge don't like C++ code intruding on their quiet glade of contemplation of "anything but talk about C++".
Jun 25, 2022 at 6:36pm Jun 25, 2022 at 6:36pm UTC
This is now an exercise to create the most optimized class for reading words from a file that you can. Go!
...anyone?
-Albatross
Jun 25, 2022 at 7:07pm Jun 25, 2022 at 7:07pm UTC
I'd need 15 feet of rubber tubing, 5 gallons of lubricant and a Yak to do it.
Jun 25, 2022 at 7:22pm Jun 25, 2022 at 7:22pm UTC
but first define what is a 'word'.
Jun 25, 2022 at 10:43pm Jun 25, 2022 at 10:43pm UTC
I usually just open the file with something like word or notepad and start reading it. I don't see why you need to program.
Last edited on Jun 25, 2022 at 10:43pm Jun 25, 2022 at 10:43pm UTC
Jun 26, 2022 at 9:50am Jun 26, 2022 at 9:50am UTC
Without using regex and considering - as a splitter char between 2 words, then consider (without file open error detection):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#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) {
static constexpr char 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), [](unsigned char ch) {return ch == split || std::isalpha(ch); });
for (const auto & 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 (const auto & 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
https://wandbox.org/permlink/zc0f2MZeFvZeegiP
Last edited on Jun 26, 2022 at 10:15am Jun 26, 2022 at 10:15am UTC