![](/img/trans.png)
[英]How to move the file pointer to next character through “ifstream” without “getting” any character, just like “fseek” does?
[英]ifstream to get then next character
因此,我有一段時間來檢查文件中的每個字符。
while (infile.get(ch))
問題在於,在同一循環中,我必須檢查下一個字符以進行某些驗證。
無論如何,我可以在跟蹤當前角色的同時移動下一個角色嗎?
謝謝
使用peek()
方法,這樣您就可以在提取下一個字符之前對其進行查看。 這是一個例子:
std::istringstream iss("ABC");
for (char c; iss.get(c); )
{
std::cout << "Current character is: " << c << std::endl;
if (iss.peek() != EOF)
std::cout << "Next character is: " << (char)iss.peek();
}
這應該輸出:
Current character is: A
Next character is: B
Current character is: B
Next character is: C
Current character is: C
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.