J'ai une entrée d'un fichier avec plusieurs délimiteurs par exemple
years,(7),(9)
years,(8),(3)
quelles méthodes puis-je utiliser pour les séparer en tant que
years
7
9
years
8
3
J'ai essayé d'utiliser strok mais je n'ai rien affiché avec ce qui suit.
getline (myfile,line, "," );
line = strtok (pch," (),");
j'ai eu cet exemple de http://www.cplusplus.com/reference/clibrary/cstring/strtok/
Réponses:
2 pour la réponse № 1Cela ressemble à un travail pour std :: locale et son fidèle acolyte imprégner:
#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 pour la réponse № 2
Vous n'avez pas utilisé strtok
droite:
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, " ,.-");
}
Voir http://www.cplusplus.com/reference/clibrary/cstring/strtok/ pour plus d'informations.