Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9966 > unrolled thread

ifstream ofstream ?

Started byAsger-P <junk@asger-p.dk>
First post2011-09-09 12:16 +0200
Last post2011-09-10 10:23 +0100
Articles 12 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  ifstream ofstream ? Asger-P <junk@asger-p.dk> - 2011-09-09 12:16 +0200
    Re: ifstream ofstream ? Goran <goran.pusic@gmail.com> - 2011-09-09 03:56 -0700
      Re: ifstream ofstream ? Asger-P <junk@asger-p.dk> - 2011-09-09 13:18 +0200
        Re: ifstream ofstream ? Goran <goran.pusic@gmail.com> - 2011-09-09 06:00 -0700
          Re: ifstream ofstream ? Asger-P <junk@asger-p.dk> - 2011-09-09 15:17 +0200
            Re: ifstream ofstream ? Goran <goran.pusic@gmail.com> - 2011-09-09 06:55 -0700
              Re: ifstream ofstream ? Asger-P <junk@asger-p.dk> - 2011-09-09 16:22 +0200
                Re: ifstream ofstream ? Goran <goran.pusic@gmail.com> - 2011-09-09 07:39 -0700
                  Re: ifstream ofstream ? Stuart Redmann <DerTopper@web.de> - 2011-09-09 08:05 -0700
                    Re: ifstream ofstream ? Goran <goran.pusic@gmail.com> - 2011-09-09 08:18 -0700
                  Re: ifstream ofstream ? Asger-P <junk@asger-p.dk> - 2011-09-09 17:07 +0200
                    Re: ifstream ofstream ? Nobody <nobody@nowhere.com> - 2011-09-10 10:23 +0100

#9966 — ifstream ofstream ?

FromAsger-P <junk@asger-p.dk>
Date2011-09-09 12:16 +0200
Subjectifstream ofstream ?
Message-ID<op.v1jdhqlu2juju3@ajwin7>
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

[toc] | [next] | [standalone]


#9971

FromGoran <goran.pusic@gmail.com>
Date2011-09-09 03:56 -0700
Message-ID<82c9e0b4-cd70-49ef-a2c5-976dbcaa88aa@o26g2000vbi.googlegroups.com>
In reply to#9966
On Sep 9, 12:16 pm, Asger-P <j...@asger-p.dk> 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.

[toc] | [prev] | [next] | [standalone]


#9972

FromAsger-P <junk@asger-p.dk>
Date2011-09-09 13:18 +0200
Message-ID<op.v1jgcklg2juju3@ajwin7>
In reply to#9971
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

[toc] | [prev] | [next] | [standalone]


#9977

FromGoran <goran.pusic@gmail.com>
Date2011-09-09 06:00 -0700
Message-ID<c66c4e0d-777c-4415-bac9-24160ff19bb7@y21g2000yqk.googlegroups.com>
In reply to#9972
On Sep 9, 1:18 pm, Asger-P <j...@asger-p.dk> 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.

[toc] | [prev] | [next] | [standalone]


#9979

FromAsger-P <junk@asger-p.dk>
Date2011-09-09 15:17 +0200
Message-ID<op.v1jlvvxx2juju3@ajwin7>
In reply to#9977
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>::operator!() 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

[toc] | [prev] | [next] | [standalone]


#9981

FromGoran <goran.pusic@gmail.com>
Date2011-09-09 06:55 -0700
Message-ID<360b4ee2-dcc1-4667-867f-0865836e5ada@l4g2000vbv.googlegroups.com>
In reply to#9979
On Sep 9, 3:17 pm, Asger-P <j...@asger-p.dk> 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>::operator!() 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.

[toc] | [prev] | [next] | [standalone]


#9983

FromAsger-P <junk@asger-p.dk>
Date2011-09-09 16:22 +0200
Message-ID<op.v1jovhwc2juju3@ajwin7>
In reply to#9981
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

[toc] | [prev] | [next] | [standalone]


#9986

FromGoran <goran.pusic@gmail.com>
Date2011-09-09 07:39 -0700
Message-ID<ba101f66-eb32-4fdb-b7cd-b566d3c61fda@d18g2000yqm.googlegroups.com>
In reply to#9983
On Sep 9, 4:22 pm, Asger-P <j...@asger-p.dk> 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.

[toc] | [prev] | [next] | [standalone]


#9987

FromStuart Redmann <DerTopper@web.de>
Date2011-09-09 08:05 -0700
Message-ID<2b02757e-99fb-41b8-bdc1-21254c7b7063@l4g2000vbv.googlegroups.com>
In reply to#9986
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

[toc] | [prev] | [next] | [standalone]


#9989

FromGoran <goran.pusic@gmail.com>
Date2011-09-09 08:18 -0700
Message-ID<2ec67aec-b7c9-4bcb-98d7-4e48f5a25761@l2g2000vbn.googlegroups.com>
In reply to#9987
On Sep 9, 5:05 pm, Stuart Redmann <DerTop...@web.de> 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.

[toc] | [prev] | [next] | [standalone]


#9988

FromAsger-P <junk@asger-p.dk>
Date2011-09-09 17:07 +0200
Message-ID<op.v1jqynep2juju3@ajwin7>
In reply to#9986
Hi Goran

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

> On Sep 9, 4:22 pm, Asger-P <j...@asger-p.dk> 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

[toc] | [prev] | [next] | [standalone]


#10018

FromNobody <nobody@nowhere.com>
Date2011-09-10 10:23 +0100
Message-ID<pan.2011.09.10.09.23.08.169000@nowhere.com>
In reply to#9988
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web