문제

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