質問

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