CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    What is the reason of file open failure?

    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.

    PHP Code:
    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!
    InfraRed.
    Please rate my post, if it helped you.

  2. #2
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: What is the reason of file open failure?

    - Sreehari
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
    " Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin

  3. #3
    Join Date
    Mar 2006
    Posts
    1

    Re: What is the reason of file open failure?

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

  4. #4
    Join Date
    Apr 1999
    Location
    Jerusalem, Israel
    Posts
    304

    Re: What is the reason of file open failure?

    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?
    InfraRed.
    Please rate my post, if it helped you.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: What is the reason of file open failure?

    Check the variable "errno" (#include <cerrno>) for the reason code or use strerror to get a textual explanation.
    Code:
    #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:
    Code:
           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
    Last edited by treuss; March 24th, 2006 at 07:15 AM.

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: What is the reason of file open failure?

    Quote Originally Posted by treuss
    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:pen 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.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured