繁体   English   中英

多个分隔符

[英]Multiple delimiters

我有一个来自具有多个分隔符的文件的输入

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

这看起来像是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";
  }
}

你没有使用正确的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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM