ifstream ofstream ?

A

Asger-P

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
 
G

Goran

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.
 
A

Asger-P

Hi Goran

Thanks for replying

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
 
G

Goran

Hi Goran

Thanks for replying




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



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.


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.
 
A

Asger-P

Hi Goran

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
 
G

Goran

Hi Goran



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.
 
A

Asger-P

Hi Goran

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
 
G

Goran

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.
 
S

Stuart Redmann

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
 
A

Asger-P

Hi Goran

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
 
G

Goran

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.
 
N

Nobody

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top