gpt4 book ai didi

c++ - 按 int 读入文件,然后是单词

转载 作者:行者123 更新时间:2023-11-30 02:52:05 25 4
gpt4 key购买 nike

我有一个类似的文本文件

1\t words words words
2\t 单词单词单词单词

其中 # 是行 #,后跟制表符,然后是随机单词

我需要读入 int,存储它,然后跳过\t,单独读入每个单词,同时跟踪单词的位置。

我希望我可以使用 getline(file, word, ' ') 和一个计数器,但它会将我的第一个单词抓取为 1\t words

如有任何帮助或建议,我们将不胜感激。

最佳答案

使用stringstreamgetline,

getline(file, line);
std::stringstream ssline(line);
int num;
ssline >> num;
std::string word;
while(ssline >> word){
// do whatever you want.
}

关于c++ - 按 int 读入文件,然后是单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com