473,583 Members | 3,094 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2417

"Mike Austin" <mi**@mike-austin.com> schrieb im Newsbeitrag
news:Wx******** *************@b gtnsc04-news.ops.worldn et.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_ope n() 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%c gl%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******** *************@b gtnsc04-news.ops.worldn et.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_ope n() 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******** *************@b gtnsc04-news.ops.worldn et.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
8482
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 have. So, when ios::badbit occurs? If I have a damaged media what occurs? Is this patform / compiler depended?
0
2689
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 to Design mode, I can close the query. I've installed SP-3, which I thought would cure the problem, to no avail. It usually happens after I've...
1
2679
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 Detail1_Print(): if I answer "No" to the dialog the report just runs. If I answer "Yes", it pops a new dialog seemingly at every line. Code is...
3
6039
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 from a database and manipulate the TypeConverters dynamically. The property grid then displays the property with a drop down list. It does work...
8
6790
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
5007
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 char* stx = &filex; inf.open(stx,ios_base::in|std::ios::binary); if(!inf.good()) { inf.clear();
7
3469
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. ResetLogin.asp (where user will have to enter their username, old password, and new password). <TR> <TH ALIGN=LEFT> <FONT...
11
13476
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 string is contained. From reading through posts to this list it seems that failbit gets set if there is a format error whilst reading. Is it bad form...
4
5873
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 file, and write it out to another. The input files are named: 0node0, 0node1, 0node2, 2node0, 2node1, 2node2 in this example. Any files that aren't...
0
7890
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7926
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8187
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5367
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1149
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.