Имам вход от файл с множество разделители, например
years,(7),(9)
years,(8),(3)
какви методи мога да използвам, за да ги разделя като
years
7
9
years
8
3
Опитах се да използвам strok, но аз нищо не показва със следното.
getline (myfile,line, "," );
line = strtok (pch," (),");
аз имам този пример от http://www.cplusplus.com/reference/clibrary/cstring/strtok/
Отговори:
2 за отговор № 1Това изглежда като работа за STD :: локал и неговия доверен помощник насаждам:
#include <locale>
#include <iostream>
struct punct_ctype : std::ctype<char> {
punct_ctype() : std::ctype<char>(get_table()) {}
static mask const* get_table()
{
static mask rc[table_size];
rc[" "] = std::ctype_base::space;
rc["n"] = std::ctype_base::space;
rc["("] = std::ctype_base::space;
rc[")"] = std::ctype_base::space;
rc[","] = std::ctype_base::space;
return &rc[0];
}
};
int main() {
using std::string;
using std::cin;
using std::locale;
cin.imbue(locale(cin.getloc(), new punct_ctype));
string word;
while(cin >> word) {
std::cout << word << "n";
}
}
0 за отговор № 2
Не си го използвал strtok
дясно:
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string "%s" into tokens:n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%sn",pch);
pch = strtok (NULL, " ,.-");
}
виждам http://www.cplusplus.com/reference/clibrary/cstring/strtok/ за повече информация.