new fstream isn't a weirdo, is it?

  • Follow


Hi all,

I got a 'problem' with the following code (using STL):

ifstream * f = new ifstream("filename.bmp", ios_base::binary);

It would create the object (f is different from NULL), but it wouldn't
pass the parameters to the constructor (f->is_open() returns false
even though the file exists, etc.) as I expected.

Is this a bug/feature of my compiler or something more standard in
C++?

Hector.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply hhcalderon 7/16/2003 8:45:08 PM

 >  >
 > What mkaes you think the parameters never amde it to the constructor?
 > Any chance the file is protected against you or otherwise not openable.
 > Any chance you screwed up the filename?  Can you open it with stdio
 > (fopen)?
 >

Because
   ifstream * f = new ifstream("filename.bmp", ios_base::binary);
doesn't work, but
   ifstream * f = new ifstream;
   f->open("filename.bmp", ios_base::binary);
does work!!!

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply hhcalderon 7/18/2003 12:02:05 PM


Ray Lischner <rl.news@tempest-sw.com> writes:

> On Wednesday 16 July 2003 01:45 pm, Hector wrote:
>
>> ifstream * f = new ifstream("filename.bmp", ios_base::binary);
>
> Among the file modes, you must supply ios_base::in, ios_base::out, or
> both.

No. 27.8.1.6/2 specifies that this ifstream constructor will bitwise
     or the supplied mode with ios_base::in . So ios_base::in is
     uneccessary. I can't figure out what the effect of supplying
     ios_base::out is supposed to be, but I bet it is undefined.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply llewelly 7/21/2003 7:30:53 PM

In article <1d7a.3f16b366.8261c@prospero.island.local>,
Ray Lischner wrote:
> On Wednesday 16 July 2003 01:45 pm, Hector wrote:
> 
>> ifstream * f = new ifstream("filename.bmp", ios_base::binary);
> 
> Among the file modes, you must supply ios_base::in, ios_base::out, or
> both.

The standard says that the ifstream constructor will add the ios_base::in
mode, and similarly the ofstream constructor will add the ios_base::out
mode, so this should not be necessary.  In practice I believe there are
some implementations that fail to do this.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 7/21/2003 8:11:18 PM

Ray Lischner <rl.news@tempest-sw.com> wrote in message
news:<1d7a.3f16b366.8261c@prospero.island.local>...
> On Wednesday 16 July 2003 01:45 pm, Hector wrote:

> > ifstream * f = new ifstream("filename.bmp", ios_base::binary);

> Among the file modes, you must supply ios_base::in, ios_base::out, or
> both.

According to the standard, ifstream is supposed to or ios::in in.

--
James Kanze           GABI Software        mailto:kanze@gabi-soft.fr
Conseils en informatique orient�e objet/     http://www.gabi-soft.fr
                    Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kanze 7/21/2003 11:35:58 PM

hhcalderon@yahoo.com (Hector) wrote in message news:<e64dc7b5.0307151607.36af0a73@posting.google.com>...
> Hi all,
> 
> I got a 'problem' with the following code (using STL):
> 
> ifstream * f = new ifstream("filename.bmp", ios_base::binary);
> 
> It would create the object (f is different from NULL), but it wouldn't
> pass the parameters to the constructor (f->is_open() returns false
> even though the file exists, etc.) as I expected.
> 
> Is this a bug/feature of my compiler or something more standard in
> C++?

After reading your kind replies, it looks like I found yet another bug
in a Microsoft product!!!

Hector C.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply hhcalderon 7/23/2003 11:13:06 PM

In article <e64dc7b5.0307221930.5c746da2@posting.google.com>,
 on 23 Jul 2003 19:13:06 -0400,
 hhcalderon@yahoo.com (Hector) wrote:

> hhcalderon@yahoo.com (Hector) wrote in message
> news:<e64dc7b5.0307151607.36af0a73@posting.google.com>...
> > Hi all,
> > 
> > I got a 'problem' with the following code (using STL):
> > 
> > ifstream * f = new ifstream("filename.bmp", ios_base::binary);
> > 
> > It would create the object (f is different from NULL), but it wouldn't
> > pass the parameters to the constructor (f->is_open() returns false
> > even though the file exists, etc.) as I expected.
> > 
> > Is this a bug/feature of my compiler or something more standard in
> > C++?
> 
> After reading your kind replies, it looks like I found yet another bug
> in a Microsoft product!!!

Whch header are you including (<fstream> or <fstream.h>)?

 The version of std::ifstream which ships with MSVC 6 "does the right
thing" (see line 264 of <fstream>). I can't say for sure what the "old"
(fstream.h) ifstream implementation does (or, for that matter, what it's
supposed to do :)

Regards,
 Andy S
-- 
"Light thinks it travels faster than anything but it is wrong. No matter
 how fast light travels it finds the darkness has always got there first,
 and is waiting for it."                  -- Terry Pratchett, Reaper Man

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Andy 7/26/2003 1:41:22 PM

6 Replies
588 Views

(page loaded in 0.082 seconds)

Similiar Articles:












7/25/2012 12:41:05 AM


Reply: