按 int 读取文件,然后按单词读取
Reading in a file by int, then words
我有一个类似于
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 开始重复
相关文章:
- 从字符串变量中逐字符读取单词
- 是否可以在文本文件中找到最长单词的长度,并在同一个文本文件中读取,只需 1 个 while 循环?
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- 我需要C++读取字符串数组中每个单词的第一个字母
- 如何读取文件的内容,如果文件中的单词重复,请将它们添加到列表中?
- 如何读取特定单词之间的文本文件?
- 无法读取文本文件中的每个单词
- C 读单词读取文件,而没有任何符号
- C :按行读取txt文件的读数,单词用单词读取,每行的单词不均匀
- 从文件重载运算符>>读取单词
- 从文本文件(c++)中读取单词和数字
- 读取单词并将它们存储到数组中
- 如何从 std::istringstream 读取单词而不通过 >> 分配给 tmp 变量?
- 从字符串中读取单词C++,同时忽略空格、数字和符号
- 为什么我的ifstream程序以错误的顺序读取单词
- 从文件中逐字符读取单词列表,并将其存储在数组中
- 从输入中读取单词序列。使用"Quit"终止,按输入顺序打印,不要打印两次单词。
- 从一个c++文本文件中读取单词(奇怪的字符)