So say I want to open a text document for reading using an instance of ifstream.open, etc.. etc... So once i open the txt file to read from it, what functions do i use to scan each character and put each character into an an array slot? Ive seen loops of if( i = 0, i < 20, i ++0) etc, but is there anyone who can walk me through it with a little more detail? Thanks

Recommended Answers

All 5 Replies

Why do you want to read a character at a time? Why not use std::getline() to read a line at a time into an std::string?

i did not know there was a getline, how many chars will it default out to, because i also want to do this in binary mode as well with things other then text documents. ill do some testing with that. But also for more technical dirty work, is there a scanchar function or something similar? Something that i can research on msdn.

There are lots of tools available. You can read one char at a time aor you can read delimited text that may be stored as text or converted to numerical types. You can create code to read in a defined group of variables using a single written command. You can read a certain number of bytes at a time. You can read files stored in binary format or as text. There is a Bitset class in the standard template library. Etc.

There are 2 versions of getline(), one for C style strings, that have to have a predifined upper size limit, and one for STL strings that don't need a predifined upper size limit, though they probably have an upper size limit defined someplace that I've never bothered to track down since I've never tried to write something that big. getline() needs some way to now when to stop. It may be the amount of data to put in (C style strings), it may a specified char, or it may be the end of file indicator. The default delimiting char for both versions of getline() is the new line char.

You can learn about all of this stuff by finding a good tutorial or picking up a good reference book. I feel (a) good reference book(s) is/are invaluable if you are serious about writing code, even if it's just for your own personal enjoyment. There are a number of options listed in the sticky post section of this board.

Move file pointer to the end of file using seekg(); use tellg() to get the length of file. Create char pointer variable; allocates memory to accomodate file content (i.e. length of file). Move file pointer to begin of file and start to read character by character using get() till end of file.

>Move file pointer to the end of file using seekg(); use tellg() to get the length of file.
Not likely. While the file is opened in text mode, the seek and tell functions do not necessarily deal in pure byte offsets from the beginning of the file.
Read this FAQ: http://www.c-faq.com/stdio/textvsbinary.html third paragraph

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.