Dragonfly Q&A

How should I test for the "eof" string?

0 like 0 dislike
I am currently writing the while loop that reads a frame from a given file and adds it into a sprite, but I just can't figure out what I should use as a test condition for the loop.  Is there a function like peek() that allows me to look for a string instead of the next character, or can I chain peek() so I can look at the next couple of characters, i.e. ifstream.peek().peek()?
asked Sep 27, 2014 by csgillis

1 Answer

0 like 0 dislike
The code that reads in a frame doesn't need to check for the "eof" string.  It knows the number of rows it expects in the frame (from parsing the header) and just reads that many rows, adding each to the frame.  If it doesn't find enough rows (i.e., the file runs out) that is an error.

Once the frame is read, the loadSprite() method expects to find the string "end".  If it doesn't, that is an error.

It knows how many frames are supposed to be in the sprite (from parsing the header), so it loops the above code the right number of times.
 
Once all the frames are read, the loadSprite() method exepcts to find the string "eof".  If it doesn't, that is an error.

So, there is no real reason to need a "peek".  After parsing the header, the loadSprite() method always knows what to expect: frame line (the right width), "end" or "eof".  If it doesn't get the right string (or right sized string), it logs an error message and returns an error.
answered Sep 28, 2014 by claypool
More information about Dragonfly can be found on the Dragonfly Home Page.
...