witam,
jak odczytać z pliku tekst z pominięciem nawiasów i przecinków?
mam taki tekst w pliku:
(marek, o, ewa)
i chce zapisać "marek", "o", "ewa" do zmiennych, bez ( i ,.
Męczę się z tym i nic.
Proszę o pomoc.
witam,
jak odczytać z pliku tekst z pominięciem nawiasów i przecinków?
mam taki tekst w pliku:
(marek, o, ewa)
i chce zapisać "marek", "o", "ewa" do zmiennych, bez ( i ,.
Męczę się z tym i nic.
Proszę o pomoc.
Spróbuj getline oraz parametra delim
Coś w ten deseń powinno być dość wygodne.
#include <iostream>
#include <sstream>
#include <string>
// jakąś ładniejsza funkcję sobie napiszesz :P
std::string trim_chars(const std::string& str, const std::string& chars_to_trim = " \n\t\r") {
std::string s = str;
std::string::size_type i = 0;
while (i < s.size() && chars_to_trim.find(s[i]) != std::string::npos) {
s.erase(i++, 1);
}
i = s.size() - 1;
while (i < s.size() && chars_to_trim.find(s[i]) != std::string::npos) {
s.erase(i--, 1);
}
return s;
}
int main() {
using namespace std;
string set;
int counter = 0;
while(getline(cin, set, '('), getline(cin, set, ')')) { // comma operator!
stringstream ss(set);
while(getline(ss, set, ',')) {
cout << ++counter << ": '" << trim_chars(set) << "'\n";
}
}
return 0;
}
$ cat test
(one, two, three, four)
(five, six,
seven, eight,
nine, ten)
(eleven and twelve,
whatever comes next)
(the end, it really is
)(no, really, this time it is)
$ ./cpp < test
1: 'one'
2: 'two'
3: 'three'
4: 'four'
5: 'five'
6: 'six'
7: 'seven'
8: 'eight'
9: 'nine'
10: 'ten'
11: 'eleven and twelve'
12: 'whatever comes next'
13: 'the end'
14: 'it really is'
15: 'no'
16: 'really'
17: 'this time it is'