我有一个类似于
1 t words words words words
2 t words words words words
其中 # is the line #
,后跟制表符,后跟随机单词
我需要在 int 中读取,存储它,然后跳过 \t,并在跟踪该单词位置的同时单独读取每个单词。
我希望我能用getline(file, word, ' ')
和一个计数器,但这抓住了我的第一个词作为1 t words
。
任何帮助或建议将不胜感激。
使用stringstream
和getline
,
getline(file, line);
std::stringstream ssline(line);
int num;
ssline >> num;
std::string word;
while(ssline >> word){
// do whatever you want.
}
假设你不能说一行有多少个单词,那么简单的答案是分两步完成。
- 使用 getline 将整行读入字符串
- 将该字符串放入 std::istringstream 中,然后从 std::istringstream 中读取行号和以下单词
然后从步骤 1 开始重复