0

例如,我有来自具有多个分隔符的文件的输入

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/得到这个例子

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 回答