Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ifstream ofstream ?

Reply
Thread Tools

ifstream ofstream ?

 
 
Asger-P
Guest
Posts: n/a
 
      09-09-2011

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
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      09-09-2011
On Sep 9, 12:16*pm, Asger-P <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Asger-P
Guest
Posts: n/a
 
      09-09-2011

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
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      09-09-2011
On Sep 9, 1:18*pm, Asger-P <(E-Mail Removed)> 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.
 
Reply With Quote
 
Asger-P
Guest
Posts: n/a
 
      09-09-2011

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>: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
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      09-09-2011
On Sep 9, 3:17*pm, Asger-P <(E-Mail Removed)> 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>: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.
 
Reply With Quote
 
Asger-P
Guest
Posts: n/a
 
      09-09-2011

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
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      09-09-2011
On Sep 9, 4:22*pm, Asger-P <(E-Mail Removed)> 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.
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      09-09-2011
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

 
Reply With Quote
 
Asger-P
Guest
Posts: n/a
 
      09-09-2011

Hi Goran

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

> On Sep 9, 4:22 pm, Asger-P <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Suddenly Can't Use "ifstream"s and "ofstream"s KevinSimonson C++ 4 11-04-2010 02:07 PM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
ifstream / ofstream question (using libstdc++ v3) phoenix C++ 1 01-13-2005 04:39 PM
Problem which ifstream and ofstream class in VC++ Armando C++ 2 01-23-2004 03:50 PM
writes to files passed as struct of ofstream objects fail m vaughn C++ 0 07-11-2003 01:06 AM



Advertisments