Is there a way we can decrement a file pointer. I searched online and I found the ungetc() function but what if we want to decrement the file pointer to point to the last string in the database? Also, how does ungetc put back a character?? Explain. Thanks in advance!!

Recommended Answers

All 2 Replies

Check out fseek() to smartly maneuver around a file. Any time the records of the database are not fixed in their size, (that is, all the same size), you will find it more difficult finding your way around inside a file.

Conceptually, there is a tiny buffer, than can be used with ungetc(). Nothing is actually "put back" into the input stream, it's just put into the buffer, and a flag tells the next access to check that tiny buffer first. The actual implementation is left up to the compiler maker.

This is not a factual understanding, but my understanding of it. I'm sure you could Google and get better info.

Check out fseek() to smartly maneuver around a file.

Note that arbitrary seeking on a stream opened in text mode is technically undefined. To use anything except an offset of 0 or the result of a previous ftell() call, the stream must be opened in binary mode.

Also, how does ungetc put back a character?? Explain.

If you're feeling adventurous enough to look at an actual implementation (it's written with clarity in mind), check out these links:

FILE structure
ungetc
fgetc

You'll see that a separate unget buffer is used to hold pushed back characters, where the buffer takes priority over the primary character buffer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.