 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mike Austin Guest
|
Posted: Wed Sep 29, 2004 1:50 am Post subject: Why isn't failbit reset on close() or open()?? |
|
|
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
|
Posted: Wed Sep 29, 2004 1:15 pm Post subject: Re: Why isn't failbit reset on close() or open()?? |
|
|
"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
|
Posted: Wed Sep 29, 2004 6:07 pm Post subject: Re: Why isn't failbit reset on close() or open()?? |
|
|
"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
|
Posted: Wed Sep 29, 2004 7:22 pm Post subject: Re: Why isn't failbit reset on close() or open()?? |
|
|
"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 |
|
 |
|
|
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
|
|