Click to See Complete Forum and Search --> : What is the reason of file open failure?
infrared
March 24th, 2006, 04:56 AM
hello,
I'm programming a simulation of a disk.
I was told to use the try/catch mechanism to catch I/O errors.
This is a part of a function i'm using,
dskfl is an instance of fstream.
void disk::mountdisk(String sFilename)
{
dskfl.open(sFilename.GetBuffer(),ios::in);
if (!dskfl.is_open())
{
//what to put here?
}
Sector sec;
dskfl >> sec;
What do i need to put in the !is_open(), if i want to throw an exception (with the reason of the error!!!) to the caller of the function?
Thanks!
sreehari
March 24th, 2006, 05:43 AM
Check this FAQ
[17] Exceptions and error handling (http://www.parashift.com/c++-faq-lite/exceptions.html)
satchi gowda
March 24th, 2006, 05:43 AM
You will be using try/catch if you are expecting something to go wrong. (Like trying to divide a number by 0) . In your case you can expect an error to occur while you try to open the file itself. But if you want to throw an exception then try this
try
{
dskfl.open(sFilename.GetBuffer(),ios::in);
if (!dskfl.is_open())
{
throw "Put your message here";
}
}
catch(char *str)
{
cout << "Exception raised: " << str << '\n';
}
infrared
March 24th, 2006, 05:48 AM
Thanks, but how I determine what the problem was in the open operation?
let's say i'm opening a file the errors that can happend:
1. file not found
2. file already opened
3. some kind of other I/O errors...
how can I throw the correct reason?
treuss
March 24th, 2006, 06:08 AM
Check the variable "errno" (#include <cerrno>) for the reason code or use strerror to get a textual explanation. #include <fstream>
#include <iostream>
#include <cerrno>
using namespace std;
int main( int argc, char* argv[] )
{
ifstream f;
f.open( argv[1] );
if ( ! f.is_open() ) {
cout << "Error opening file: " << strerror(errno) << endl;
}
}
So, either you just create an exception that includes the errno, or you make a big switch statement based on errno and throw specific exceptions like FileNotFound and PermissionDenied. The list of error codes may differ on different OSes, but below is an example: E2BIG Arg list too long
EACCES Permission denied
EADDRINUSE
Address in use
EADDRNOTAVAIL
Address not available
EAFNOSUPPORT
Address family not supported
EAGAIN Resource temporarily unavailable
EALREADY
Connection already in progress
EBADF Bad file descriptor
EBADMSG
Bad message
EBUSY Resource busy
ECANCELED
Operation canceled
ECHILD No child processes
ECONNABORTED
Connection aborted
ECONNREFUSED
Connection refused
ECONNRESET
Connection reset
EDEADLK
Resource deadlock avoided
EDESTADDRREQ
Destination address required
EDOM Domain error
EDQUOT Reserved
EEXIST File exists
EFAULT Bad address
EFBIG File too large
EHOSTUNREACH
Host is unreachable
EIDRM Identifier removed
EILSEQ Illegal byte sequence
EINPROGRESS
Operation in progress
EINTR Interrupted function call
EINVAL Invalid argument
EIO Input/output error
EISCONN
Socket is connected
EISDIR Is a directory
ELOOP Too many levels of symbolic links
EMFILE Too many open files
EMLINK Too many links
EMSGSIZE
Inappropriate message buffer length
EMULTIHOP
Reserved
ENAMETOOLONG
Filename too long
ENETDOWN
Network is down
ENETRESET
Connection aborted by network
ENETUNREACH
Network unreachable
ENFILE Too many open files in system
ENOBUFS
No buffer space available
ENODATA
No message is available on the STREAM head read queue
ENODEV No such device
ENOENT No such file or directory
ENOEXEC
Exec format error
ENOLCK No locks available
ENOLINK
Reserved
ENOMEM Not enough space
ENOMSG No message of the desired type
ENOPROTOOPT
Protocol not available
ENOSPC No space left on device
ENOSR No STREAM resources
ENOSTR Not a STREAM
ENOSYS Function not implemented
ENOTCONN
The socket is not connected
ENOTDIR
Not a directory
ENOTEMPTY
Directory not empty
ENOTSOCK
Not a socket
ENOTSUP
Not supported
ENOTTY Inappropriate I/O control operation
ENXIO No such device or address
EOPNOTSUPP
Operation not supported on socket
EOVERFLOW
Value too large to be stored in data type
EPERM Operation not permitted
EPIPE Broken pipe
EPROTO Protocol error
EPROTONOSUPPORT
Protocol not supported
EPROTOTYPE
Protocol wrong type for socket
ERANGE Result too large
EROFS Read-only file system
ESPIPE Invalid seek
ESRCH No such process
ESTALE Reserved
ETIME STREAM ioctl() timeout
ETIMEDOUT
Operation timed out
ETXTBSY
Test file busy
EWOULDBLOCK
Operation would block (may be same value as EAGAIN)
EXDEV Improper link
SuperKoko
March 24th, 2006, 06:18 AM
Check the variable "errno" (#include <cerrno>) for the reason. The list of error codes may differ on different OSes, but below is an example:
fstream::open may not modify the errno variable (even if it may on some implementations).
However, it is still possible to search for file existence if the file has not been opened.
fstreams does not provide much information on the type of error which occured. :(
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.