Re: Reading Files (was: Reading Files and ASCII Code)
Dustin Ventin wrote:
I actually have two questions.
Then make two postings, for the sake of clearness. ;)
The first involves reading from a normal everyday txt file.
There is no such thing. Everyday HTML comes encoded as ISO8859-1, everyday
XML comes encoded as UTF-8, everyday C++ sourcecode come (on many
platforms) encoded as ASCII. Other files might use other encodings.
I need to be able to read data from a text file a variable number of
characters at a time.
I may need to read in one character at one point, or three, or five. Is
there a function that allows me to say: "read in x characters from where
the pointer is now in the file"?
Assuming you are using C++ IOStreams, the position for a read is always
implicitly the current position. You could use read() or readsome() or even
one of the get() overloads. Be aware that istreams by default skip
whitespace, so you need to explicitly tell it not to do so. Also, by
default, files are handled as textfiles, i.e. CR/LF conversion is done -
use the ios_base::binary flag to avoid that. You might want to skip
iostreams alltogether and go to the lower-level streambuffers instead -
these are used by the streams to do (almost) just the character IO and no
formatting or interpretation.
What about moving the pointer in the file back a couple characters?
No problem, there are functions for seeking in a file.
Information on these fuctions and how to use them would be invaluable.
Yes, see http://msdn.microsoft.com or any better C++ book (in case you don't
have one, check out the book reviews at accu.org).
Uli