473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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;
};

Jul 22 '05 #1
3 2404

"Mike Austin" <mi**@mike-austin.com> schrieb im Newsbeitrag
news:Wx*********************@bgtnsc04-news.ops.worldnet.att.net...
It's the most annoying thing, and causes hours of frustration. Why
can't it
be resolved?

Regards,
Mike Austin

Example:

#include <iostream> #include <fstream> here. #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::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 <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::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
Jul 22 '05 #2
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2r*************@uni-berlin.de...
"Mike Austin" <mi**@mike-austin.com> schrieb im Newsbeitrag
news:Wx*********************@bgtnsc04-news.ops.worldnet.att.net...
#include <iostream>

#include <fstream> here.
#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::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 <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::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

Jul 22 '05 #3

"Mike Austin" <mi**@mike-austin.com> wrote in message
news:Wx*********************@bgtnsc04-news.ops.worldnet.att.net...
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/wg...ctive.html#409

john
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
What is the difference between ios::failbit and ios::badbit? When happen the first and when the second? I try several circumstances to describe when every flag occurs but only ios::failbit I...
0
by: YFS DBA | last post by:
I'm getting an error message when I open queries then try to close them: "This action will reset the current code in break mode" Clicking on 'Yes' just brings up the same message. If I change...
1
by: PeteCresswell | last post by:
I'm working on a new report in an MS Access DB. The first anomaly was a message "This action will reset the current code in break mode." when the report was run. Seems TB something about my...
3
by: Dave Girvitz | last post by:
I have a PropertyGrid (Windows Forms App) based component that uses TypeConverters to generate ranges of acceptable values for properties. The idea was that I could download the key/value pairs...
8
by: John J. Hughes II | last post by:
Is there a way of resetting the SqlConnection with closing/opening it again. I am not seeing anything called reset. Regards, John
1
by: SantaClaus | last post by:
Hi all, Im getting an exception in the middle of reading a file and here is my code std::ifstream inf; const int bufSize=1024; char tbuffer; std::string filex = path; // may be C:\test.txt const...
7
by: hotflash | last post by:
Hi All, I want to creat a script where I will allow user to reset their own password. I have tried different options but don't have any luck. Wonder what I want to do is kinda not valid or not. ...
11
by: rory | last post by:
I am reading a binary file and I want to search it for a string. The only problem is that failbit gets set after only a few calls to getline() so it never reaches the end of the file where the...
4
by: IanWright | last post by:
I'm having trouble with getline function, in the sense that I keep getting an error, and I'm wondering if anyone can help? The code I'm using is below. What it does is to read a line from one...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.