C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why isn't failbit reset on close() or open()??

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Mike Austin
Guest





PostPosted: Wed Sep 29, 2004 1:50 am    Post subject: Why isn't failbit reset on close() or open()?? Reply with quote



It's the most annoying thing, and causes hours of frustration. Why can't it
be resolved?

Regards,
Mike Austin

Example:

#include <iostream>
#include <string>

using namespace std;

class DataStore {
public:
Store( string filename ) {
datafile.open( filename.c_str(), ios::in | ios::binary );
if( !_datafile ) {
datafile.clear(); // Will not work correctly without clearing
datafile.open( filename.c_str(), ios::out | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::out | ios::binary );
private:
fstream datafile;
};

Back to top
Gernot Frisch
Guest





PostPosted: Wed Sep 29, 2004 1:15 pm    Post subject: Re: Why isn't failbit reset on close() or open()?? Reply with quote




"Mike Austin" <mike (AT) mike-austin (DOT) com> schrieb im Newsbeitrag
news:Wxo6d.644365$Gx4.110798 (AT) bgtnsc04-news (DOT) ops.worldnet.att.net...
Quote:
It's the most annoying thing, and causes hours of frustration. Why
can't it
be resolved?

Regards,
Mike Austin

Example:

#include <iostream
#include #include <string

using namespace std;

class DataStore {
public:
Store( string filename ) {

function declaration with no return type?

Quote:
datafile.open( filename.c_str(), ios::in | ios::binary );
if( !_datafile ) {

'_datafile' is not declared. is it 'datafile' ?
if (datafile) will always return 'true', since datafile is a valid
object. Try datafile.is_open() here.


Quote:
datafile.clear(); // Will not work correctly without clearing

// why not calling datafile.close() before re-opening it?

Quote:
datafile.open( filename.c_str(), ios::out | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::out |
ios::binary );

Here, a '}' is missing.


Quote:
private:
fstream datafile;
};

corrected code:


#include #include <string>

using namespace std;

class DataStore
{
public:
void Store( string filename )
{
datafile.open( filename.c_str(), ios::in | ios::binary );
if(!datafile.is_open())
{
datafile.clear(); // Will not work correctly without clearing
datafile.open( filename.c_str(), ios::out | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::out | ios::binary );
}
private:
fstream datafile;
};

HTH,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com



Back to top
Mike Austin
Guest





PostPosted: Wed Sep 29, 2004 6:07 pm    Post subject: Re: Why isn't failbit reset on close() or open()?? Reply with quote



"Gernot Frisch" <Me (AT) Privacy (DOT) net> wrote

Quote:
"Mike Austin" <mike (AT) mike-austin (DOT) com> schrieb im Newsbeitrag
news:Wxo6d.644365$Gx4.110798 (AT) bgtnsc04-news (DOT) ops.worldnet.att.net...
#include <iostream
#include #include
using namespace std;

class DataStore {
public:
Store( string filename ) {

function declaration with no return type?

datafile.open( filename.c_str(), ios::in | ios::binary );
if( !_datafile ) {

'_datafile' is not declared. is it 'datafile' ?
if (datafile) will always return 'true', since datafile is a valid
object. Try datafile.is_open() here.


datafile.clear(); // Will not work correctly without clearing

// why not calling datafile.close() before re-opening it?

datafile.open( filename.c_str(), ios::out | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::out |
ios::binary );

Here, a '}' is missing.


private:
fstream datafile;
};

corrected code:


#include #include
using namespace std;

class DataStore
{
public:
void Store( string filename )
{
datafile.open( filename.c_str(), ios::in | ios::binary );
if(!datafile.is_open())
{
datafile.clear(); // Will not work correctly without clearing
datafile.open( filename.c_str(), ios::out | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::out | ios::binary );
}
private:
fstream datafile;
};

I'm sorry, I did a little editing before I posted and broke the code! The
point is that you must use clear() before tying to open a file after failing
in a previous attempt. close() or any other method has no effect. It just
seems wrong.. calling open() should clear the failbit flag. I've been using
C++ for years, mostly with OpenGL, and hate when I run into these
time-wasting issues.

Mike Austin


Back to top
John Harrison
Guest





PostPosted: Wed Sep 29, 2004 7:22 pm    Post subject: Re: Why isn't failbit reset on close() or open()?? Reply with quote


"Mike Austin" <mike (AT) mike-austin (DOT) com> wrote

Quote:
It's the most annoying thing, and causes hours of frustration. Why can't
it
be resolved?


I've heard it described as an oversight. As to why it can't be resolved,
well I think it is being, but slowly.

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#409

john



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.