Thread: Reading file using ifstream

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Reading file using ifstream

    I'm trying to read the contents of a file using the ifstream constructs. I've written a program that is a carbon copy off the website, under Reading From a File:

    C++ Binary File I/O

    Code:
    1 #include <fstream>
    2 using namespace std;
    3 int main() {
    4 char buffer[100];
    5 ifstream myFile("temp", ios::in);
    6 myFile.read(buffer, 100);
    7 if(!myFile) {
    8         fprintf(stderr, "Fail\n");
    9 }
    10 }
    There is a file called temp in the directory where the program was built and the executable is handled. Everytime I execute the program, it prints "Fail." The file temp has chmod 777.

    The heck is going wrong here?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can check for more specifics about the error in several ways:
    1. Immediately after line 5, you can check whether the file even opened.
    2. If it did open, you can be more specific than just !myFile -- is at end of file (eof) or did it encounter a read error (bad)?
    3. You can also use perror or errno to get the error message from the operating system.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    myFile.is_open returns 1
    myFile.good returns 1
    myFile.eof returns 0

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If myFile.good() returns true, then !myFile will be false (technically !myFile returns myFile.fail(), but if the failbit is set then good() will return false). Did you check before the read or after?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    Those results were before the read.

    After the read, open returns 1; good returns 0; end returns 1; and bad returns 0 (??)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you are at end of file, the file is no longer "good" (as you see), hence your !myFile triggers. (bad is reserved for read errors; eof is not considered a read error.) Whether or not the read was successful is another story -- you'd have to call gcount to find out if anything was read (or examine the contents of buffer, I suppose, except they started out uninitialized, so who knows whether what you have was from the file or not).

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    gcount returns 0

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So that means nothing was read; presumably this means your file exists but is empty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream file name
    By FpaFtw in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2010, 10:56 AM
  2. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  3. Reading whole file w/ ifstream
    By cboard_member in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 09:09 AM
  4. Replies: 2
    Last Post: 12-16-2001, 09:09 PM
  5. ifstream whole file
    By Road233 in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2001, 07:51 AM