ifstream ofstream ?

Discussion in 'C++' started by Asger-P, Sep 9, 2011.

  1. Asger-P

    Asger-P Guest

    Hi

    I never used ifstream and ofstream before, so I searched a little
    and made this little function, for removing BOM's in php files.
    It works fine, I just want to make sure that I'm doing it right.

    void __fastcall removeBOM(char* fileName)
    {
    ifstream ifile(fileName, ios::in | ios::binary | ios::nocreate);
    if( ! ifile )
    return;

    unsigned char Bom[3];
    ifile.seekg( 0 );
    ifile.read ( Bom, 3 );

    if( Bom[0] == 0xEF && Bom[1] == 0xBB && Bom[2] == 0xBF )
    {
    bool Done = false;
    string tmpFile = fileName;
    tmpFile += "tmptmp";

    ofstream ofile( tmpFile.c_str() );
    if( ofile )
    {
    ofile << ifile.rdbuf();
    ofile.close();
    Done = true;
    }
    ifile.close();

    //renaming and deleting files

    return;
    }

    ifile.close();
    }

    in particular this line:

    if( ! ifile )

    isn't that always true on an refference ?


    Best regards
    Asger-P
     
    Asger-P, Sep 9, 2011
    #1
    1. Advertising

  2. Asger-P

    Goran Guest

    On Sep 9, 12:16 pm, Asger-P <> wrote:
    > Hi
    >
    > I never used ifstream and ofstream before, so I searched a little
    > and made this little function, for removing BOM's in php files.
    > It works fine, I just want to make sure that I'm doing it right.
    >
    > void __fastcall removeBOM(char* fileName)
    > {
    >     ifstream ifile(fileName, ios::in | ios::binary | ios::nocreate);
    >     if( ! ifile )
    >     return;
    >
    >     unsigned char Bom[3];
    >     ifile.seekg( 0 );
    >     ifile.read ( Bom, 3 );
    >
    >     if( Bom[0] == 0xEF && Bom[1] == 0xBB && Bom[2] == 0xBF )
    >     {
    >        bool Done = false;
    >        string tmpFile = fileName;
    >        tmpFile += "tmptmp";
    >
    >        ofstream ofile( tmpFile.c_str() );
    >        if( ofile )
    >        {
    >           ofile << ifile.rdbuf();
    >           ofile.close();
    >           Done = true;
    >        }
    >        ifile.close();
    >
    >        //renaming and deleting files
    >
    >        return;
    >     }
    >
    >     ifile.close();
    >
    > }
    >
    > in particular this line:
    >
    > if( ! ifile )
    >
    > isn't that always true on an refference ?


    First off: what reference?

    Second, try this:

    class test
    {
    };

    test t;
    test& r = t;
    if (r) // doesn't compile
    {
    }

    I suggest to step through that "if" (not "step over") with your
    debugger and think about what you're seeing. (I am not trying to be
    facetious, I am certain you'll learn something this way)

    Goran.
     
    Goran, Sep 9, 2011
    #2
    1. Advertising

  3. Asger-P

    Asger-P Guest

    Hi Goran

    Thanks for replying

    On the: 09. of september-2011 At: 12:56 Goran wrote:

    >>
    >> if( ! ifile )
    >>
    >> isn't that always true on an refference ?

    >
    > First off: what reference?


    You know what I'm talking about, so please tell me, that isn't
    a reference that is called ????

    > Second, try this:


    > class test
    > {
    > };
    >
    > test t;
    > test& r = t;
    > if (r) // doesn't compile
    > {
    > }


    neither does this:
    if(t)
    {
    }
    so reference or ???? that doesn't matter, it does of course give me
    clue in direction of, there is something special about ifile...
    If there wasn't it would not compile.

    > I suggest to step through that "if" (not "step over") with your
    > debugger and think about what you're seeing. (I am not trying to be
    > facetious, I am certain you'll learn something this way)


    That shows me what I expected to see a whole lot of internal
    values of ifile: {{3,false, false,0,512,0,.......
    but nothing that makes me any wiser


    Best regards
    Asger-P
     
    Asger-P, Sep 9, 2011
    #3
  4. Asger-P

    Goran Guest

    On Sep 9, 1:18 pm, Asger-P <> wrote:
    > Hi Goran
    >
    > Thanks for replying
    >
    > On the: 09. of september-2011 At: 12:56 Goran wrote:
    >
    >
    >
    > >> if( ! ifile )

    >
    > >> isn't that always true on an refference ?

    >
    > > First off: what reference?

    >
    > You know what I'm talking about, so please tell me, that isn't
    > a reference that is called ????
    >
    > > Second, try this:
    > > class test
    > > {
    > > };

    >
    > > test t;
    > > test& r = t;
    > > if (r) // doesn't compile
    > > {
    > > }

    >
    > neither does this:
    > if(t)
    > {}
    >
    > so reference or ???? that doesn't matter, it does of course give me
    > clue in direction of, there is something special about ifile...
    > If there wasn't it would not compile.
    >
    > > I suggest to step through that "if" (not "step over") with your
    > > debugger and think about what you're seeing. (I am not trying to be
    > > facetious, I am certain you'll learn something this way)

    >
    > That shows me what I expected to see a whole lot of internal
    > values of ifile: {{3,false, false,0,512,0,.......
    > but nothing that makes me any wiser


    Go to the "if" line with your debugger. Once there, issue "step into"
    command. You should see something strange. You should step into some
    function from standard library. Take a good look at what the function
    is.

    Goran.
     
    Goran, Sep 9, 2011
    #4
  5. Asger-P

    Asger-P Guest

    Hi Goran

    On the: 09. of september-2011 At: 15:00 Goran wrote:

    >
    > Go to the "if" line with your debugger. Once there, issue "step into"
    > command. You should see something strange. You should step into some
    > function from standard library. Take a good look at what the function
    > is.


    Ok I see this:
    basic_ios<charT, traits>::eek:perator!() const
    {
    return fail();
    }

    I couldn't go into fail(), but I found this:

    template<class charT, class traits>
    inline bool
    basic_ios<charT, traits>::fail() const
    {
    return (__state & (ios_base::failbit | ios_base::badbit));
    }

    So it look like it returns whether or not everything went ok,
    so that is fine then.

    Thanks.

    P.s. Is the code all together OK ?
    and You didn't tell me what the correct name was,
    now that reference wasn't the right one?

    Thanks again.
    Best regards
    Asger-P
     
    Asger-P, Sep 9, 2011
    #5
  6. Asger-P

    Goran Guest

    On Sep 9, 3:17 pm, Asger-P <> wrote:
    > Hi Goran
    >
    > On the: 09. of september-2011 At: 15:00 Goran wrote:
    >
    >
    >
    > > Go to the "if" line with your debugger. Once there, issue "step into"
    > > command. You should see something strange. You should step into some
    > > function from standard library. Take a good look at what the function
    > > is.

    >
    > Ok I see this:
    >    basic_ios<charT, traits>::eek:perator!() const
    >    {
    >      return fail();
    >    }


    THAT's what I wanted you to see (but you missed it :-(). That is why
    your "if" compiles: because there's operator! on your stream object.
    So in the end, this operator says whether the stream is fine or not.

    There's no reference in your code. Clearly, ifile is not a reference
    (to a variable), it IS a variable.

    Goran.
     
    Goran, Sep 9, 2011
    #6
  7. Asger-P

    Asger-P Guest

    Hi Goran

    On the: 09. of september-2011 At: 15:55 Goran wrote:

    > THAT's what I wanted you to see (but you missed it :-().


    Ahh.

    it might have helped if You had written:

    I suggest to step into that "if"

    instead of:

    I suggest to step through that "if"

    but only might have. ;-)


    > There's no reference in your code. Clearly, ifile is not a reference
    > (to a variable), it IS a variable.


    if it was an int or so I would have called it that, I just
    didn't know You used the same name for classes as well.

    Thanks for Your help.
    Best regards
    Asger-P
     
    Asger-P, Sep 9, 2011
    #7
  8. Asger-P

    Goran Guest

    On Sep 9, 4:22 pm, Asger-P <> wrote:
    > > There's no reference in your code. Clearly, ifile is not a reference
    > > (to a variable), it IS a variable.

    >
    > if it was an int or so I would have called it that, I just
    > didn't know You used the same name for classes as well.


    What, you mean, like primitive/reference type in Java? Naaaaah... In
    my mind, there's no such distinction in C++. "int" can pretty easily
    be viewed as a class, only, it's full of operators ;-).

    Goran.
     
    Goran, Sep 9, 2011
    #8
  9. On 9 Sep., Goran wrote:
    > > > There's no reference in your code. Clearly, ifile is not a reference
    > > > (to a variable), it IS a variable.



    On Sep 9, Asger-P wrote:
    > > if it was an int or so I would have called it that, I just
    > > didn't know You used the same name for classes as well.



    On 9 Sep., Goran wrote:
    > What, you mean, like primitive/reference type in Java? Naaaaah... In
    > my mind, there's no such distinction in C++. "int" can pretty easily
    > be viewed as a class, only, it's full of operators ;-).


    Gosh, I didn't know that the Java community used different names for
    variables depending on whether a variable is of a primitive type or of
    a class type.

    Good to know that. I never would have figured out what OP might have
    meant by that.

    Thx,
    Stuart
     
    Stuart Redmann, Sep 9, 2011
    #9
  10. Asger-P

    Asger-P Guest

    Hi Goran

    On the: 09. of september-2011 At: 16:39 Goran wrote:

    > On Sep 9, 4:22 pm, Asger-P <> wrote:
    >> > There's no reference in your code. Clearly, ifile is not a reference
    >> > (to a variable), it IS a variable.

    >>
    >> if it was an int or so I would have called it that, I just
    >> didn't know You used the same name for classes as well.

    >
    > What, you mean, like primitive/reference type in Java? Naaaaah... In
    > my mind, there's no such distinction in C++. "int" can pretty easily
    > be viewed as a class, only, it's full of operators ;-).


    Oooh I just mean that I dont have a "spoken language" when
    it comes to programming, I have always worked alone and
    learned all I know from the NG's, the web and the help
    files that comes with BCB, I never talk to anybody about
    programming.

    So there is a lot of things I dont know what is called. ;-)

    In my head "ifile" is the name that refer to the instance
    of the ifstream class, so it kind of an reference, of course
    I know it is not the "reference kind" reference, You see my
    problem. ;-)
    Well You call it a variable and so it shall be called.

    Best regards
    Asger-P
     
    Asger-P, Sep 9, 2011
    #10
  11. Asger-P

    Goran Guest

    On Sep 9, 5:05 pm, Stuart Redmann <> wrote:
    > On 9 Sep., Goran wrote:
    >
    > > > > There's no reference in your code. Clearly, ifile is not a reference
    > > > > (to a variable), it IS a variable.

    >
    > On Sep 9, Asger-P wrote:
    >
    > > > if it was an int or so I would have called it that, I just
    > > > didn't know You used the same name for classes as well.

    >
    > On 9 Sep., Goran wrote:
    >
    > > What, you mean, like primitive/reference type in Java? Naaaaah... In
    > > my mind, there's no such distinction in C++. "int" can pretty easily
    > > be viewed as a class, only, it's full of operators ;-).

    >
    > Gosh, I didn't know that the Java community used different names for
    > variables depending on whether a variable is of a primitive type or of
    > a class type.


    I don't __know__ that they do (or how many of them). Please don't hold
    me on that.

    Goran.
     
    Goran, Sep 9, 2011
    #11
  12. Asger-P

    Nobody Guest

    On Fri, 09 Sep 2011 17:07:25 +0200, Asger-P wrote:

    > In my head "ifile" is the name that refer to the instance
    > of the ifstream class, so it kind of an reference, of course
    > I know it is not the "reference kind" reference, You see my
    > problem. ;-)


    In C and C++, you have primitive types (basically numbers; integer and
    floating-point types), aggregates (structures, unions and, in C++,
    classes), arrays, pointers and (in C++) references (which are basically
    pointers under the hood).

    In Java, you have primitive types and pointers (references) to class
    types. You can't have a pointer to a primitive type, nor can you have an
    instance of a class type other than via a pointer/reference.

    Hence, class types tend to be referred to as reference types. Assignment
    copies the reference, not the object to which it refers; for the latter,
    you need to use the .clone() method.

    Neither C nor C++ have this restriction. Pointers/references aren't
    required for aggregate types, nor are they restricted to them.
     
    Nobody, Sep 10, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. m vaughn
    Replies:
    0
    Views:
    424
    m vaughn
    Jul 11, 2003
  2. Armando
    Replies:
    2
    Views:
    7,069
    Martijn Lievaart
    Jan 23, 2004
  3. phoenix
    Replies:
    1
    Views:
    341
    Victor Bazarov
    Jan 13, 2005
  4. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    603
    Ivan Johansen
    Jul 14, 2005
  5. KevinSimonson
    Replies:
    4
    Views:
    363
    James Kanze
    Nov 4, 2010
Loading...

Share This Page