[英]ifstream - Move to next word
對於ifstream的功能,我還不清楚。 我已經搜索過,但找不到該問題的具體答案。
我想做的是停在某個單詞上,然后在該單詞之后做點什么。
具體來說,我的程序瀏覽包含單詞的文件。 每次看到字符串“ NEWWORD”時,都需要對“ NEWWORD”后面的單詞進行處理
string str;
ifstream file;
file.open("wordFile.txt");
while(file >> str){
//Look through the file
if(str == "NEWWORD"){
//Do something with the word following NEWWORD
}
}
file.close;
如何告訴ifstream轉到下一個單詞?
PS:對不起,如果我在准則和規則方面做錯了什么。 這是我第一次發貼。
每次使用>>
從流中提取內容時,它都會自動前進到下一項(這是while
循環不斷在文件中前進直到找到“ NEWWORD”的方式。您可以在看到“ NEWWORD”時提取下一項:
string str;
ifstream file;
file.open("wordFile.txt");
while(file >> str){
//Look through the file
if(str == "NEWWORD"){
//Do something with the word following NEWWORD
if (file >> str) {
// the word following NEWWORD is now in str
}
}
}
file.close;
為了闡明Matt的答案,在istream上使用>>
將找到下一個非空格字符,然后讀取找到的所有字符,直到到達空格字符或文件末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.