Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. CFileException, discover reasons RESOLVED

CFileException, discover reasons RESOLVED

Scheduled Pinned Locked Moved ATL / WTL / STL
c++visual-studiohelpcsharptools
12 Posts 2 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following: CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; } This is from the VS help utility: catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); } This leads to the concept if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.

    Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

    L B 3 Replies Last reply
    0
    • B bkelly13

      Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following: CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; } This is from the VS help utility: catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); } This leads to the concept if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.

      Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Use the GetErrorMessage[^] member function.

      Use the best guess

      B 2 Replies Last reply
      0
      • L Lost User

        Use the GetErrorMessage[^] member function.

        Use the best guess

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        Cool. Thank you.

        Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

        1 Reply Last reply
        0
        • L Lost User

          Use the GetErrorMessage[^] member function.

          Use the best guess

          B Offline
          B Offline
          bkelly13
          wrote on last edited by
          #4

          Oops, I have a follow up on this. Declarations:

          CFileException get_error_message;
          const unsigned int ERROR_MAX = 64;
          char char_error_text[ ERROR_MAX ];
          LPTSTR ptr_error_text = &char_error_text[0];
          CString final_error_message = "";

          Code:

          m_definition_file_name += "x";
          try
          {
          m_definition_file.Open( m_definition_file_name, CFile::modeRead );
          get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
          final_error_message = char_error_text;
          error_number = GetLastError();

          }
          catch( CFileException e )
          { code }
          catch( ... )
          { code }
          {

          The line m_definition_file_name += "x" forces the file name to be incorrect. Char array char_error_text winds up with "No error occurred." while error_number gets 0 (zero) When the line error_number = GetLastErrror is moved directly beneath the open command, it get the value 2 (two). But that consumes the last error and get_error_message does not get anything. Nothing is thrown and neither catch is activated. What do I need to change to use this CFileException?

          Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

          L 1 Reply Last reply
          0
          • B bkelly13

            Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following: CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; } This is from the VS help utility: catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); } This leads to the concept if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.

            Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

            B Offline
            B Offline
            bkelly13
            wrote on last edited by
            #5

            I have also tried this. Declarations:

            const unsigned int ERROR_MAX = 64;
            char char_error_text[ ERROR_MAX ];
            LPTSTR ptr_error_text = &char_error_text[0];
            CString final_error_message = "";

            With this code

            m_definition_file.Open( m_definition_file_name, CFile::modeRead );
            error_number = GetLastError();
            format_message_return = FormatMessage( 0, // dwFlags
            NULL, // lpSousrce
            error_number, // dwMessageID
            NULL, // dwLanguageId
            ptr_error_text, // lpBuffer
            ERROR_MAX, // nSize
            NULL ); // Arguments
            final_error_message = char_error_text;

            The error number is 2 (two). char_error_text ends up with nothing. I am uncertain of the arguments for FormatMessage. The file name is forced to be incorrect and an error is expected.

            Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

            1 Reply Last reply
            0
            • B bkelly13

              Oops, I have a follow up on this. Declarations:

              CFileException get_error_message;
              const unsigned int ERROR_MAX = 64;
              char char_error_text[ ERROR_MAX ];
              LPTSTR ptr_error_text = &char_error_text[0];
              CString final_error_message = "";

              Code:

              m_definition_file_name += "x";
              try
              {
              m_definition_file.Open( m_definition_file_name, CFile::modeRead );
              get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
              final_error_message = char_error_text;
              error_number = GetLastError();

              }
              catch( CFileException e )
              { code }
              catch( ... )
              { code }
              {

              The line m_definition_file_name += "x" forces the file name to be incorrect. Char array char_error_text winds up with "No error occurred." while error_number gets 0 (zero) When the line error_number = GetLastErrror is moved directly beneath the open command, it get the value 2 (two). But that consumes the last error and get_error_message does not get anything. Nothing is thrown and neither catch is activated. What do I need to change to use this CFileException?

              Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I don't understand what you are doing here; it should be:

              try
              {
              // code to open the CFile
              }
              catch (CFileException ce)
              {
              TCHAR message[132];
              if (ce.GetErrorMessage(message, 132, NULL)
              {
              // display the contents of the message buffer
              }
              }

              Use the best guess

              B 1 Reply Last reply
              0
              • L Lost User

                I don't understand what you are doing here; it should be:

                try
                {
                // code to open the CFile
                }
                catch (CFileException ce)
                {
                TCHAR message[132];
                if (ce.GetErrorMessage(message, 132, NULL)
                {
                // display the contents of the message buffer
                }
                }

                Use the best guess

                B Offline
                B Offline
                bkelly13
                wrote on last edited by
                #7

                Hello, I don't see what you have different. Second, when the file does not exist, this Open method does not throw an exception, so none of the try/catch matters. I had a catch(...) at the bottom and it was not entered.

                Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                L 1 Reply Last reply
                0
                • B bkelly13

                  Hello, I don't see what you have different. Second, when the file does not exist, this Open method does not throw an exception, so none of the try/catch matters. I had a catch(...) at the bottom and it was not entered.

                  Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  If the open function does not throw an error then you need to check the return value and call GetLastError() to find out why, as described here[^]. Alternatively you can throw a new exception like:

                  try
                  {
                  if (!m_definition_file.Open(m_definition_file_name, CFile::modeRead))
                  {
                  DWORD error_number = GetLastError();
                  throw new CFileException::OsErrorToException(error_number);
                  }
                  }
                  catch( CFileException e )
                  {
                  // handle the exception
                  }

                  Note that when calling GetLastError() you must call it immediately after the failing API function call, otherwise its return may not be valid.

                  Use the best guess

                  1 Reply Last reply
                  0
                  • B bkelly13

                    Windows 7, 64 bit, Visual Studio 2008, MFC, C++ After a long series of searches looking for a nice method of reading from a text file into CString I arrived at the following: CStdioFile definition_file; try { definition_file.Open( m_definition_file_name, CFile::modeRead ); } catch( CFileException e ) { e.m_cause; } This is from the VS help utility: catch( CFileException* e ) { if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n") e->Delete(); } This leads to the concept if( e->m_cause == CFileException::fileNotFound ) printf( "ERROR: File not found\n"); elseif( e->m_cause == CFileException::generic ) printf( "ERROR: An unspecified error occurred.\n"); elseif etc etc Is there a better way of capturing the text of the error rather than an if/else/else/else sequence? Note: I am open to any type of suggestion. The goal is to read a CSV text file and parse it into a std::map.

                    Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                    B Offline
                    B Offline
                    bkelly13
                    wrote on last edited by
                    #9

                    I have yet to get a text based error code for opening a file. Here is the simplified code:

                    m_definition_file_name = Select_Definition_File.GetPathName();
                    m_definition_file_name += "x"; // make the file name bad
                    m_definition_file.Open( m_definition_file_name, CFile::modeRead );

                    // error_number = GetLastError(); // restore to verify error code is 2
                    get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
                    final_error_message = char_error_text; // says no error

                    The Open method does not throw an exception. When GetLasError() is used, the error code is 2 (two). The GetErrorMesssage yields No error returned, which is incorrect. The file could not be opened because it does not exist. GetErrorMessage(...) is has three arguments about formatting the error message, but the error message is not an argument. I presume it gets the error message like GetLastError() does, but this appears incorrect. How do I get a text string describing this error?

                    Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                    L 1 Reply Last reply
                    0
                    • B bkelly13

                      I have yet to get a text based error code for opening a file. Here is the simplified code:

                      m_definition_file_name = Select_Definition_File.GetPathName();
                      m_definition_file_name += "x"; // make the file name bad
                      m_definition_file.Open( m_definition_file_name, CFile::modeRead );

                      // error_number = GetLastError(); // restore to verify error code is 2
                      get_error_message.GetErrorMessage( ptr_error_text, ERROR_MAX, NULL );
                      final_error_message = char_error_text; // says no error

                      The Open method does not throw an exception. When GetLasError() is used, the error code is 2 (two). The GetErrorMesssage yields No error returned, which is incorrect. The file could not be opened because it does not exist. GetErrorMessage(...) is has three arguments about formatting the error message, but the error message is not an argument. I presume it gets the error message like GetLastError() does, but this appears incorrect. How do I get a text string describing this error?

                      Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      I gave you a suggested solution at http://www.codeproject.com/Messages/4583835/Re-CFileException-discover-reasons.aspx[^], which should throw an exception whose value would be CFileException::fileNotFound (error code 2). Did you try it, and if so what result did you get? In response to your comment about FormatMessage, you need to tell the function where to look for the mappings of error codes to messages thus:

                      format_message_return = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // dwFlags
                      NULL, // lpSousrce
                      error_number, // dwMessageID
                      NULL, // dwLanguageId
                      ptr_error_text, // lpBuffer
                      ERROR_MAX, // nSize
                      NULL ); // Arguments

                      This is explained in full detail in the documentation[^].

                      Use the best guess

                      B 1 Reply Last reply
                      0
                      • L Lost User

                        I gave you a suggested solution at http://www.codeproject.com/Messages/4583835/Re-CFileException-discover-reasons.aspx[^], which should throw an exception whose value would be CFileException::fileNotFound (error code 2). Did you try it, and if so what result did you get? In response to your comment about FormatMessage, you need to tell the function where to look for the mappings of error codes to messages thus:

                        format_message_return = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // dwFlags
                        NULL, // lpSousrce
                        error_number, // dwMessageID
                        NULL, // dwLanguageId
                        ptr_error_text, // lpBuffer
                        ERROR_MAX, // nSize
                        NULL ); // Arguments

                        This is explained in full detail in the documentation[^].

                        Use the best guess

                        B Offline
                        B Offline
                        bkelly13
                        wrote on last edited by
                        #11

                        My apologies for no replying sooner. I followed your advice and now have that FormatMessage method working. Thank you.

                        Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                        L 1 Reply Last reply
                        0
                        • B bkelly13

                          My apologies for no replying sooner. I followed your advice and now have that FormatMessage method working. Thank you.

                          Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          You're welcome.

                          Use the best guess

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups