Thread: Question about fstream

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    Question about fstream

    I have a question about using ifstream.
    From text book, it tells me we can use like following
    (1)
    Code:
        ifstream infile;
        infile.open("abc.txt");
        if(!infile)
           cerr << "Error: uanable to open input file" << endl;
    (2)
    However, I just codes from some others using ifstreams*..
    and I modify (1) to
    Code:
        ifstream* infile;
        infile->open("abc.txt");
        if(!(*infile))
           cerr << "Error: uanable to open input file" << endl;
    Both pass compiling process, but somehow if abc.txt doesn't exist, then
    (1) shows the correct cerr message
    (2) shows "Segmenation fault"

    Actually I don't quite get the meaning of fstream
    Isn't (2) same as (1)?
    Can I think of ifstream like a file pointer? Then it reads EOF or other error which returns false value in if(infile).. And ifstream* is a pointer pointer to a file pointer?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In the second case you don't have an object. You only have an uninitialized pointer pointing to some random garbage.

    Can I think of ifstream like a file pointer?
    You could, but it is rather an object managing a file pointer. You must have a fstream object to call open in the first place.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Thx for replay, anon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. FStream Question
    By CPP-Null in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2003, 01:28 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. fstream question!
    By FutureCoder in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2003, 04:06 PM