How do I read a .txt file character by character?

In my program, I'm supposed to read a text file (the name of which is given to me as a command line paramater, as long with an integer), and display the text in a specific format (each line can only be as long as the integer). However, I'm having trouble even reading the text file. I don't know the syntax. I'm only allowed to edit the function that does the formatting, and the code in that is

void typeset (int maxWidth, istream& documentIn)

I don't know how to 'read' the file, as most examples online are ifstream, or openFile or something like that. What I want to do is just read the first character of the file, and continuously keep reading characters until the end of the file. Can anyone please teach me how to do this?
closed account (Dy7SLyTq)
I said in my original post that most of the examples I found used ifstream, openFile, or something other than istream&. I don't know the syntax for using istream&, which is why I posted the thread in the first place.
ifstream is a descendand of istream. std::cin is a instance of istream. So just read it as you would read data from std::cin. Opening of file should be handled outside of typeset function and as you said you cannot edit anything aside from that function, it should be handled already.
And because of the inheritence relationship, you can pass an ifstream to a function which expects an istream.

const int maxWidth = 80; // edit: correct variable name

// assume we've including what's needed and are using std;

ifstream documentIn("test.doc");

if (documentIn.is_open()) {
    // pass ifstream to typeset's istream param
    typeset(maxWidth, documentIn);
} else {
    cerr << "error : could not open file\n";

When it comes to reading the values, it's the same whether the data comes from a file, the console/terminal or memory (e.g. istringstream.)

Last edited on
Google strtok
toad1359 wrote:
Google strtok

I don't see any place for strtok in this problem.

By the way, do you have to read the stream char by char? Or can you use an extraction operator? (e.g. documentIn >> value; )

Last edited on
closed account (Dy7SLyTq)
@andy: look at line 1 of your code

@op: i know what you said, but you dont understand ifstream. it inherits from istream get, so you can do ifstream object.get(). there is no "syntax" for istream, its just a class for io
Get a line of text from the file assign it to a string and then

mystring = strtok(input_line, "");
Last edited on
Read OP post again then again. And again until you notice that he does not need tokenisation at all.

Besides, his problem now is reading from file. It is too early to speak about string manipulation. Also strtok does not work with std::string and using c-string withoun urgent need (communication between programs, binary files manipulation) is detrimental to clean and proper C++ programming.
Last edited on
DTSCode wrote:
@andy: look at line 1 of your code

Thanks - have fixed dozy mis-copy and paste.

Topic archived. No new replies allowed.