Quote:Original post by Gage64
Quote:Original post by Sneftel
Quote:Original post by Gage64
But wouldn't such a function also need to use skipComments()?
No. There's no such thing as skipping. Skipping is simply what happens when you fail to take an action as a result of reading something. If I read three numbers, then add the second and third ones and print the result, have I not skipped the first number?
Maybe I'm missing the point, but if you want to read three numbers from a text file, you have to separate the characters that belong to the numbers from the characters that don't. This is what I refer to as skipping and is the functionality that I'm trying to implement.
So, I don't understand what you're trying to say here.
No, no. Listen carefully.
Suppose I have a file which consists of the following text, in its entirety: "a3d4ba6gh". I am responsible for adding up all the numbers (which are assumed to each be one digit), while ignoring all letters.
Here's the first way I can do it: I write a function SkipLetters, which tries to discard any letters from the stream, without killing any digits. I write a function GetChar, which gets the next char and returns it. I call these two functions alternately, each time assuming the result of GetChar is a digit (because I've discarded any letters). The problem is implementing SkipLetters, assuming I don't want to use ungetc.
Here's the second way I can do it: I write a function GetDigitOnly, which, in a loop, tries to read a digit from the stream. Each time it fails to get a digit (because it has instead gotten a letter) it just tries again. When it successfully reads a digit, it returns it. I simply call this function until I run out of file. And I didn't need to use ungetc.
Note that this is exactly what EC's code is doing, except with recursion converted to iteration.