Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Get the contents of a file, character by character

  1. Dec 30, 2007 #1
    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.
     
  2. jcsd
  3. Dec 30, 2007 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Can you post what you have so far?
     
  4. Dec 30, 2007 #3
    o right. sorry.
    Code (Text):

    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).
     
  5. Dec 30, 2007 #4

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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 (Text):
    b_file >> c;
    cout << c;
    would change into something like
    Code (Text):
    while(!b_file.eof()) {
    b_file >> c;
    cout << c;
    }
    but for the exact syntax you should look at the documentation a bit.
     
  6. Dec 30, 2007 #5

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Also, you might want to use getline() instead of just streaming in the file.
    Check out the examples on file I/O with fstreams on this page
     
  7. Dec 30, 2007 #6

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

    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
    }
     
     
  8. Dec 30, 2007 #7

    jtbell

    User Avatar

    Staff: Mentor

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

    and illustrates it with the following code:

    Code (Text):

    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 (Text):

    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 (Text):

    string word;
    while (myfile >> word)
    {
        cout << word << endl;
    }
     
     
  9. Jan 1, 2008 #8
    Thank you very much guys.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?