0

たとえば、複数の区切り記号を持つファイルからの入力があります

years,(7),(9)
years,(8),(3)

それらを分離するためにどのような方法を使用できますか

years
7
9
years
8
3

ストロークを使用しようとしましたが、次のように何も表示されません。

getline (myfile,line, ',' );
line = strtok (pch," (),");

この例はhttp://www.cplusplus.com/reference/clibrary/cstring/strtok/から入手しました

4

2 に答える 2

2

これはstd::localeと彼の信頼できる相棒imbueの仕事のようです:

#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";
  }
}
于 2012-11-14T14:59:48.457 に答える
0

strtokあなたは正しく使用しませんでした:

char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
  printf ("%s\n",pch);
  pch = strtok (NULL, " ,.-");
}

詳細については、 http://www.cplusplus.com/reference/clibrary/cstring/strtok/を参照してください。

于 2012-11-14T15:08:37.460 に答える