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

M

Mike Austin

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::eek:ut | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::eek:ut | ios::binary );
private:
fstream datafile;
};
 
G

Gernot Frisch

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

Regards,
Mike Austin

Example:

#include <iostream>
#include said:
#include <string>

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::eek:ut | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::eek:ut |
ios::binary );

Here, a '}' is missing.

private:
fstream datafile;
};

corrected code:


#include <fstream>
#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::eek:ut | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::eek:ut | 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
 
M

Mike Austin

Gernot Frisch said:
function declaration with no return type?


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



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


Here, a '}' is missing.



corrected code:


#include <fstream>
#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::eek:ut | ios::binary );
datafile.write( "what the?", 9 );
}
datafile.close();
datafile.open( filename.c_str(), ios::in | ios::eek:ut | 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
 
Joined
Dec 8, 2007
Messages
1
Reaction score
0
stop whining

Why can't this be resolved? or This is simply unacceptable! when i hear that I just think... wow congrats, fix it your self but stop crying to us. It simply is unacceptable then cast your vote by programming in java, or c# or help get it fixed. Sorry to be less than constructive.
 

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