So I have a while look that checks every character in a file.

while (infile.get(ch))

The problem is that in the same loop I have to check the next character to check for some validation.

Is there anyway I could move the next character while keeping track of the current one?

Thanks

有帮助吗?

解决方案

Use the peek() method so you will be able to look at the next character before extracting it. Here's an example:

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();
}

This should output:

 Current character is: A
 Next character is: B
 Current character is: B
 Next character is: C
 Current character is: C
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top