Get the contents of a file, character by character

  • Thread starter Thread starter Cinimod
  • Start date Start date
  • Tags Tags
    File
AI Thread Summary
The discussion centers on reading a file character by character in C and C++, while also storing each character, including whitespace, in an array. The initial code provided attempts to read from a file but stops at the first whitespace due to using the extraction operator (>>), which skips whitespace. To correctly read each character, the use of the `get()` member function is recommended, allowing for the retrieval of all characters, including spaces and newlines. Participants highlight the misconception surrounding the `eof()` function, clarifying that it only returns true after an attempt to read past the end of the file. Instead, using a loop that checks the success of input operations, such as `getline()`, is advised for reading lines until the end of the file is reached. This ensures that the program behaves correctly without processing garbage values. Overall, the conversation emphasizes the importance of understanding file I/O functions and their proper usage in C and C++.
Cinimod
Messages
34
Reaction score
0
I am stuck on a program where I need to open a file, and print out the contents to a screen, but at the same time, I want to save each character in the file (spaces included) to an element of an array. However, I am having a problem opening the file and going through it character by character (I don't know if there are any functions already available to help me with this). What I have at the moment prints off the file to screen, but stops at the first whitespace it encounters. I ideally want to be able to do it in both C and C++, but at the moment, I am having trouble just doing it in the one language.
 
Technology news on Phys.org
Can you post what you have so far?
 
o right. sorry.
Code:
int main(int argc, char *argv[])
{
int i;
char string[*argv[3]],c;

if(argc < 3) {
usage(argv[0]);
exit(1);
}

ifstream b_file(argv[2]);

b_file >> c;
cout << c;

cin.get();
}

void usage(char *cmd)
{
printf("Usage: %s <file to read with complete path> <Number of characters to scan>\n", cmd);
}
I have to admit, there isn't much of a program at the moment, but I've only just started. I know that to read it character by character, I will need to implement a for loop (hence why I declared i, since I plan to use it as an index).
 
It's been a long while since I programmed C(++) but I think there should be an EOF (end of file) function that tells you when you have reached the end of the file.
So probably the lines
Code:
b_file >> c;
cout << c;
would change into something like
Code:
while(!b_file.eof()) {
b_file >> c;
cout << c;
}

but for the exact syntax you should look at the documentation a bit.
 
Also, you might want to use getline() instead of just streaming in the file.
Check out the examples on file I/O with fstreams http://www.cplusplus.com/doc/tutorial/files.html
 
Cinimod said:
o right. sorry.
Code:
b_file >> c;

This skips whitespace (blanks, tabs, newlines). To read a file one character at a time, without skipping anything, use the istream get() member function:

Code:
while (b_file.get(c))   // the loop terminates when you try to read past end of file
{
    // do what you need to do with c here
}
 
CompuChip said:
Check out the examples on file I/O with fstreams http://www.cplusplus.com/doc/tutorial/files.html

Unfortunately, that page perpetuates a very common misconception about the eof() member function:

Notice how we have used a new member function, called eof() that returns true in the case that the end of the file has been reached. We have created a while loop that finishes when indeed myfile.eof() becomes true (i.e., the end of the file has been reached).

and illustrates it with the following code:

Code:
while (! myfile.eof() )
{
    getline (myfile,line);
    cout << line << endl;
}

The eof() member function actually signals "true" only after you have tried to read past the end of file and failed. In the example above, when getline() reads the last line, eof() remains "false" because getline() does not try to read past the newline that terminates the last line. Therefore the loop goes around one more time, getline() tries to read past the end of file, fails, and possibly puts garbage into "line". The output statement then processes that extra garbage line before the loop cycles back to the top and only then does eof() terminate the loop.

The correct way to do this is as follows:

Code:
while (getline (myfile,line))
{
    cout << line << endl;
}

This works because in a Boolean context, getline() evaluates as "true" or "false" depending on whether the input succeeded or failed. Other standard C++ input operations behave the same way. Notice how I used get() in my preceding post. Similarly, if you want to read one "word" at a time, skipping whitespace, you can do this:

Code:
string word;
while (myfile >> word)
{
    cout << word << endl;
}
 
Thank you very much guys.
 
Back
Top