473,322 Members | 1,431 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,322 software developers and data experts.

C++ I/O error detection

If a C++ I/O operation fails, then how can I determine the cause? For
instance, in C, I'd do something like this:

FILE* file;
file = fopen("foo.txt", "r");
if (file == NULL)
{
switch(errno)
...
}

In C++, the best I can come up with is

istream in(foo.txt);
if (in.bad())
{
...
}
else if (in.fail())
{
...
}

However, this doesn't tell me why the error occurred; just that
something went wrong.

Also, bad() is supposed to be true on a fatal error, and fail() on a
non-fatal error. What are considered fatal and non-fatal errors?

Thanks,
Rennie
Jul 23 '05 #1
9 3107
I am not sure if the standard mandates fopen indicate/set any
meaningfull error codes.

look up "errno" in ERRNO.H it might be the most portable solution.
Raj

Jul 23 '05 #2
ra******@hotmail.com wrote:
I am not sure if the standard mandates fopen indicate/set any
meaningfull error codes.

look up "errno" in ERRNO.H it might be the most portable solution.


I know about errno for C I/O functions. I wanted to know how to get the
same information using the C++ I/O classes. Do they set errno, too?

Rennie
Jul 23 '05 #3
> I know about errno for C I/O functions. I wanted to know how to get the
same information using the C++ I/O classes. Do they set errno, too?


They are under no obligation to do so.

Stephen Howe
Jul 23 '05 #4
Look into is_open()

"Rennie deGraaf" <ca.ucalgary.cpsc@degraaf> wrote in message
news:DStTd.495866$8l.161518@pd7tw1no...
If a C++ I/O operation fails, then how can I determine the cause? For
instance, in C, I'd do something like this:

FILE* file;
file = fopen("foo.txt", "r");
if (file == NULL)
{
switch(errno)
...
}

In C++, the best I can come up with is

istream in(foo.txt);
if (in.bad())
{
...
}
else if (in.fail())
{
...
}

However, this doesn't tell me why the error occurred; just that
something went wrong.

Also, bad() is supposed to be true on a fatal error, and fail() on a
non-fatal error. What are considered fatal and non-fatal errors?

Thanks,
Rennie

Jul 23 '05 #5
Please don't top-post, and please don't do full-quotes.

Winbatch wrote:
In C++, the best I can come up with is

istream in(foo.txt);
if (in.bad())
{
...
}
else if (in.fail())
{
...
}

However, this doesn't tell me why the error occurred; just that
something went wrong.


Look into is_open()


How would that tell you the cause of the error?
Jul 23 '05 #6
Rennie deGraaf wrote:
ra******@hotmail.com wrote:
I am not sure if the standard mandates fopen indicate/set any
meaningfull error codes.

look up "errno" in ERRNO.H it might be the most portable solution.
I know about errno for C I/O functions.


I think his point is more: What useful requirements about the contents
of 'errno' does the C standard make? Hint: none. The only known error
which may be stored in 'errno' under some situations is 'EILSEQ'.
There are various places where an implementation defined value is set
in 'errno' but this does not really provide solid grounds for a
platform
independent error reporting strategy. Of course, for stdio other
standards, e.g. POSIX, cover some error cases, at least.
I wanted to know how to get the same information using the C++ I/O
classes. Do they set errno, too?


It depends on whom you ask :-) If you asked me, I would say that the
C++ standard indeed mandates setting of 'errno' in certain cases! It
does not do so explicitly, though, but by reference: various function
in IOStreams are described to behave "as if" they called a C library
function. Of course, this includes setting 'errno' to the appropriate
value. Unfortunately, if you'd asked other implementers of the
standard IOStreams library, they disagree. The funny thing is that
most of these implementations still actually set 'errno'! This is
because the underlying system functions, e.g. POSIX's open(2), read(2),
write(2), etc. set 'errno' and nobody bothers to reset 'errno' after
these calls. That is, it is common behavior of the implementations
and arguably specified by the standard but most implementers actually
disagree that this is what the standard says although there is no
direct alternative in C++! If somebody would file a defect on this
issue ("reasons for errors not accessible for stream" or something
like this) we would at least get some clarification.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #7
Dietmar Kuehl wrote:
Rennie deGraaf wrote:
ra******@hotmail.com wrote:
> I am not sure if the standard mandates fopen indicate/set any
> meaningfull error codes.
>
> look up "errno" in ERRNO.H it might be the most portable solution.


I know about errno for C I/O functions.


I think his point is more: What useful requirements about the contents
of 'errno' does the C standard make? Hint: none. The only known error
which may be stored in 'errno' under some situations is 'EILSEQ'.
There are various places where an implementation defined value is set
in 'errno' but this does not really provide solid grounds for a
platform independent error reporting strategy.


Actually, it does. You can use strerror to get a string describing the
error, and then you can display that string to the user. So whether the
file does not exist or the permissions aren't right or whatever else, you
don't need to care in your program. You can just abort the operation that
was to be done and tell the user why it failed so he can fix it and try
again. In C++ streams, there is nothing like that. You can only tell the
user something like "Couldn't open file", which IMHO is not an appropriate
error message in a modern program.
Jul 23 '05 #8
Rolf Magnus wrote:
Actually, it does. You can use strerror to get a string describing the error, and then you can display that string to the user.
Indeed... I haven't thought of this. Of course:
In C++ streams, there is nothing like that. You can only tell the
user something like "Couldn't open file", which IMHO is not an appropriate error message in a modern program.


.... you may have noticed that I'm maintaining the opinion that the
current standard already mandates setting of 'errno' in IOStreams.
Since there is currently no better alternative we should either

- clarify the standard to make it crystal clear that 'errno' is set
in the appropriate places in IOStreams.
- provide a "better" approach than 'errno'.

Is somebody filing a defect report on this issue? I would champion
the position that some details on the reasons of failures are
necessary in a portable way, although not necessarily by 'errno'
if there is a better alternative...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #9
Dietmar Kuehl wrote:
... you may have noticed that I'm maintaining the opinion that the
current standard already mandates setting of 'errno' in IOStreams.
I guess that, for now, I'll have to check if the compilers/libraries I
want to support (GCC, Sun CC, and MSVC++ 7 on Linux, OS X, Solaris, and
Win32, at least) set errno appropropriately and go with that.
Since there is currently no better alternative we should either

- clarify the standard to make it crystal clear that 'errno' is set
in the appropriate places in IOStreams.
- provide a "better" approach than 'errno'.

Is somebody filing a defect report on this issue? I would champion
the position that some details on the reasons of failures are
necessary in a portable way, although not necessarily by 'errno'
if there is a better alternative...


The "better alternative" would probably be to use exceptions. I'm not
particularly fond of Java's way of throwing a different type for every
possible error condition; I'd do something more like throw an
IOException, and include a enum type identifying the type of error; such
an enum would roughly correspond to all the values of errno that could
be set due to I/O operations.

However, I've never been fond of constructors throwing exceptions, as
catching them properly can do some nasty things to the structure of a
function/method in some cases. For instance, in Java (which uses
exceptions everywhere), if I wanted to open a bunch of sockets and be
able to tell which open failed, I could end up with something like this:
try {
Socket s1 = new Socket(host1, port1);
try {
Socket s2 = new Socket(host2, port2);
try {
Socket s3 = new Socket(host3, port3);
// do some socket I/O, and catch the associated exceptions
...
} catch (IOException e) {...}
} catch (IOException e) {...}
}catch (IOException e) {...}

Instead, I would prefer some mechanism such as returning an invalid
object on the failure of a constructor, having some mechanism to check
if a constructor succeeded, and throwing an exception if the invalid
object was accessed. Then, the above code would look more like this:
Socket s1 = new Socket(host1, port1);
Socket s2 = new Socket(host2, port2);
Socket s3 = new Socket(host3, port3);
if (s1.invalid()) {...}
if (s2.invalid()) {...}
if (s3.invalid()) {...}
// do some socket I/O, and catch the associated exceptions
....

Of course, this probably breaks OO badly enough to start some nice holy
wars among the purists. Does anyone have any better ideas?

Rennie
Jul 23 '05 #10

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

Similar topics

0
by: Cherrish Vaidiyan | last post by:
sir, The following are the steps that i followed in setting up standby database on Red hat Linux 9. i am using Oracle 9i. i have followed the steps in this site : ...
2
by: andy johnson | last post by:
I made the mistake of "upgrading" to IE6.0 on my windoze 98 laptop. I really wish bill gates would die in a horribly painful accident. Anyway, after much grief, I downloaded Opera and saw true...
2
by: Simon | last post by:
I have recently set up a server certificate on a web site. Under certain conditions I need to change the color of a html span element. I do this using the following javascript function called from...
3
by: news.onetel.net.uk | last post by:
I and my friend Karl have spent literally all day trying to find out what is causing my error but we are zapped of any further functionality :) I have a form that adds news records. You select...
7
by: debugger | last post by:
hello, Question, on page load, I populate an existing drop down with createElement and appendChild. It works fine so far. BUT I want to automatically select some option from this populated drop...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
by: JohnQ | last post by:
(The thread "Error Handling Idioms" prompted this post. I meant to post it at the top level, but it got posted as a reply. So here it is again!) An attempt at common defintions: fault: the...
2
by: coolsmaster | last post by:
When I put my code through different inputs, one form of input results in a problem. when I enter: updatename 123456789 10 f9 the expected output is "Error: 10 is out of the range 0-4" ...
3
by: harshadanarvekar | last post by:
Hi Everyone, Here is a part of javascript code that works well in FF2 but shows above error in IE6 and error "missing name after . operator" in NS8 function PopUp(idf,stepX,stepY,speed){
0
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall...
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
1
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.