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

Dani AI

Generated

A compact, practical plan for based on the thread: pick the tool for the job — line-based reads for text parsing, block reads for binary or performance, and char-by-char only when you must inspect every byte. and are right that higher-level helpers save effort; ’s size-based idea works too, but follow ’s warning about relying on seekg/tellg in text mode (use binary mode when you need exact byte counts).

Read whole text (small files) into a string:

std::ifstream in("file.txt");
std::string s((std::istreambuf_iterator<char>(in)),
               std::istreambuf_iterator<char>());

This gives you an easy random-access array of chars without manual allocation.

Read an entire binary file into a vector safely (uses binary + ate to get size):

std::ifstream in("file.bin", std::ios::binary | std::ios::ate);
if (!in) { /* handle error */ }
auto size = in.tellg();
in.seekg(0, std::ios::beg);
std::vector<char> buf(size);
in.read(buf.data(), size);

Check for errors and empty files; tellg/seekg are reliable here because the file is opened in binary mode.

Stream or scan when files are large or you need per-char logic:

std::string out;
out.reserve(4096);
char c;
while (in.get(c)) {
  // process or push_back
  out.push_back(c);
}

Or read fixed chunks with read() in a loop and use gcount() to know how many bytes were returned.

Troubleshooting tips: always check in.is_open() / in.good(); prefer std::string/std::vector over raw new/delete; open with std::ios::binary for exact byte semantics and to avoid newline translation; stream large files in chunks to avoid running out of memory.

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.