Problem with message box (causes runtime error) 
Author Message
 Problem with message box (causes runtime error)

I have a dialog which among others asks for the name of a data file. If
this file does not exist, then a message box is displayed using the
following code:

int abc=0; //to check if the following 'if' loop is called
    if (!datafile.is_open())     //checks to see if file opens successfully
    {  
        MessageBox("The file was not opened.\n Please retry", "Error
message", MB_OK | MB_ICONINFORMATION);

        abc=1; //it is not called
    }

When I execute the program, and the message box is displayed, when I
click on OK, I get a runtime error and the application shuts down.

How can I fix this? What I want to do, is return back to my dialog and
make the necessary corrections.

Thank you in advance,
Elias



Mon, 21 Feb 2005 09:08:44 GMT  
 Problem with message box (causes runtime error)
MessageBox need four parameters as below,

MessageBox(0,"The file was not opened.\n Please
    retry", "Error message", MB_OK | MB_ICONINFORMATION);

I can not reproduce your problem by running the code as
below,

// CASE.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "windows.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
        int abc=0; //to check if the following 'if' loop
is called
    if (1)     //checks to see if file opens successfully
    {  
        MessageBox(0,"The file was not opened.\n Please
retry", "Error message", MB_OK | MB_ICONINFORMATION);

        abc=1; //it is not called
    }
        return 0;

Quote:
}

can you give me more code about yr project? you can send

thanks

Quote:
>-----Original Message-----
>I have a dialog which among others asks for the name of
a data file. If
>this file does not exist, then a message box is

displayed using the
Quote:
>following code:

>int abc=0; //to check if the following 'if' loop is
called
>    if (!datafile.is_open())     //checks to see if file
opens successfully
>    {  
>        MessageBox("The file was not opened.\n Please
retry", "Error
>message", MB_OK | MB_ICONINFORMATION);

>        abc=1; //it is not called
>    }

>When I execute the program, and the message box is
displayed, when I
>click on OK, I get a runtime error and the application
shuts down.

>How can I fix this? What I want to do, is return back to
my dialog and
>make the necessary corrections.

>Thank you in advance,
>Elias

>.



Mon, 21 Feb 2005 10:10:30 GMT  
 Problem with message box (causes runtime error)

Justin,

Here is some more info about my application. Hopefully this will clear
up some things:

I have the following function which is supposed to call the
mainprogram() which contains among others the check whether the file to
be read exists and issues the message box in case that it does not.

void CDialog1Dlg::OnOK()
{
   extern bool success;  //success is false if an invalid file is selected
   mainprogram();  //Calls the function mainprogram.
                   //When this process ends, the estimator finishes.
   if(success == FALSE)
       return;            //if invalid file selected, allows user to
enter another one

CDialog::OnOK();
(then it has some other irrelevant code)

Following is my main program

void CDialog1Dlg::mainprogram()
{

   ifstream datafile (m_FileNameFull); //m_FileNameFull was read in the
Browse function

   int abc=0; //to check if the following 'if' loop is called
   if (!datafile.is_open())     //checks to see if file opens successfully
   {
       MessageBox("The file was not opened.\n Please retry", "Error
message", MB_OK | MB_ICONINFORMATION);

       abc=1; //it is not called
   }
extern bool success;
success = TRUE;
if (abc==1) success = FALSE; //returns FALSE and allows user to reenter
file (checked above in onOK() function)

Hope this helps.

Thanks for your interest,
Elias

Quote:

>MessageBox need four parameters as below,

>MessageBox(0,"The file was not opened.\n Please
>    retry", "Error message", MB_OK | MB_ICONINFORMATION);

>I can not reproduce your problem by running the code as
>below,

>// CASE.cpp : Defines the entry point for the application.
>//

>#include "stdafx.h"
>#include "windows.h"

>int APIENTRY WinMain(HINSTANCE hInstance,
>                     HINSTANCE hPrevInstance,
>                     LPSTR     lpCmdLine,
>                     int       nCmdShow)
>{
>    int abc=0; //to check if the following 'if' loop
>is called
>    if (1)     //checks to see if file opens successfully
>    {  
>        MessageBox(0,"The file was not opened.\n Please
>retry", "Error message", MB_OK | MB_ICONINFORMATION);

>        abc=1; //it is not called
>    }
>    return 0;
>}

>can you give me more code about yr project? you can send

>thanks

>>-----Original Message-----
>>I have a dialog which among others asks for the name of

>a data file. If

>>this file does not exist, then a message box is

>displayed using the

>>following code:

>>int abc=0; //to check if the following 'if' loop is

>called

>>   if (!datafile.is_open())     //checks to see if file

>opens successfully

>>   {  
>>       MessageBox("The file was not opened.\n Please

>retry", "Error

>>message", MB_OK | MB_ICONINFORMATION);

>>       abc=1; //it is not called
>>   }

>>When I execute the program, and the message box is

>displayed, when I

>>click on OK, I get a runtime error and the application

>shuts down.

>>How can I fix this? What I want to do, is return back to

>my dialog and

>>make the necessary corrections.

>>Thank you in advance,
>>Elias

>>.



Mon, 21 Feb 2005 12:23:34 GMT  
 Problem with message box (causes runtime error)
Hi Elias,

the code can work well on my machine.
this is my code i emulate yr code. in order to test it, i
put it into a MFC project named as ifstream. the code
snippet list below.

#include "stdafx.h"
#include "ifstream.h"
#include "ifstreamDlg.h"
#include <fstream.h>

class CIfstreamDlg : public CDialog
{
.....
        bool success;    //for convenience,i treat
                         //success as member var
        void mainprogram();
.....

Quote:
};

void CIfstreamDlg::OnOK()
{
  mainprogram();  //Calls the function mainprogram.
                   //When this process ends, the
                   //estimator finishes.
  if(success == FALSE)
       return;
        CDialog::OnOK();

Quote:
}

void CIfstreamDlg::mainprogram()
{
   ifstream datafile ("c:\\abcde.txt"); //c:\abce.txt
                                        //doesn't exists
   int abc=0;
   if (!datafile.is_open())  
   {
       MessageBox("The file was not opened.\n Please
retry", "Error message", MB_OK | MB_ICONINFORMATION);

       abc=1; //it is not called
   }
success = TRUE;
if (abc==1) success = FALSE;

Quote:
}

although abcde.txt doesn't exist, but datafile will
create a new file and as a result the messagebox will not
appear. is the yr m_FileNameFull similar
to "c:\\abcde.txt" ?

Quote:
>-----Original Message-----
>Justin,

>Here is some more info about my application. Hopefully
this will clear
>up some things:

>I have the following function which is supposed to call
the
>mainprogram() which contains among others the check

whether the file to
Quote:
>be read exists and issues the message box in case that
it does not.

>void CDialog1Dlg::OnOK()
>{
>   extern bool success;  //success is false if an

invalid file is selected
Quote:
>   mainprogram();  //Calls the function mainprogram.
>                   //When this process ends, the
estimator finishes.
>   if(success == FALSE)
>       return;            //if invalid file selected,
allows user to
>enter another one

>CDialog::OnOK();
>(then it has some other irrelevant code)

>Following is my main program

>void CDialog1Dlg::mainprogram()
>{

>   ifstream datafile (m_FileNameFull); //m_FileNameFull
was read in the
>Browse function

>   int abc=0; //to check if the following 'if' loop is
called
>   if (!datafile.is_open())     //checks to see if file
opens successfully
>   {
>       MessageBox("The file was not opened.\n Please
retry", "Error
>message", MB_OK | MB_ICONINFORMATION);

>       abc=1; //it is not called
>   }
>extern bool success;
>success = TRUE;
>if (abc==1) success = FALSE; //returns FALSE and allows
user to reenter
>file (checked above in onOK() function)

>Hope this helps.

>Thanks for your interest,
>Elias


>>MessageBox need four parameters as below,

>>MessageBox(0,"The file was not opened.\n Please
>>    retry", "Error message", MB_OK |

MB_ICONINFORMATION);

- Show quoted text -

Quote:

>>I can not reproduce your problem by running the code as
>>below,

>>// CASE.cpp : Defines the entry point for the
application.
>>//

>>#include "stdafx.h"
>>#include "windows.h"

>>int APIENTRY WinMain(HINSTANCE hInstance,
>>                     HINSTANCE hPrevInstance,
>>                     LPSTR     lpCmdLine,
>>                     int       nCmdShow)
>>{
>>        int abc=0; //to check if the following 'if' loop
>>is called
>>    if (1)     //checks to see if file opens
successfully
>>    {  
>>        MessageBox(0,"The file was not opened.\n Please
>>retry", "Error message", MB_OK | MB_ICONINFORMATION);

>>        abc=1; //it is not called
>>    }
>>        return 0;
>>}

>>can you give me more code about yr project? you can
send

>>thanks

>>>-----Original Message-----
>>>I have a dialog which among others asks for the name
of

>>a data file. If

>>>this file does not exist, then a message box is

>>displayed using the

>>>following code:

>>>int abc=0; //to check if the following 'if' loop is

>>called

>>>   if (!datafile.is_open())     //checks to see if
file

>>opens successfully

>>>   {  
>>>       MessageBox("The file was not opened.\n Please

>>retry", "Error

>>>message", MB_OK | MB_ICONINFORMATION);

>>>       abc=1; //it is not called
>>>   }

>>>When I execute the program, and the message box is

>>displayed, when I

>>>click on OK, I get a runtime error and the application

>>shuts down.

>>>How can I fix this? What I want to do, is return back
to

>>my dialog and

>>>make the necessary corrections.

>>>Thank you in advance,
>>>Elias

>>>.



Mon, 21 Feb 2005 13:50:30 GMT  
 Problem with message box (causes runtime error)

Justin,

The problem is that when I manually type a non-existent data file in the
edit box, I get firstly the message box asking the user to retry, and
when I click OK, I get "Runtime error", "Abnormal Program Termination."
 Then it shuts down the program.

What I need is basically to be able to go back in my dialog and be
allowed to reenter a data file.

Any ideas on that?

Quote:

>Hi Elias,

>the code can work well on my machine.
>this is my code i emulate yr code. in order to test it, i
>put it into a mfc project named as ifstream. the code
>snippet list below.

>#include "stdafx.h"
>#include "ifstream.h"
>#include "ifstreamDlg.h"
>#include <fstream.h>

>class CIfstreamDlg : public CDialog
>{
>.....
>    bool success;    //for convenience,i treat
>                         //success as member var
>    void mainprogram();
>.....
>};

>void CIfstreamDlg::OnOK()
>{
>  mainprogram();  //Calls the function mainprogram.
>                   //When this process ends, the
>                   //estimator finishes.
>  if(success == FALSE)
>       return;
>    CDialog::OnOK();
>}

>void CIfstreamDlg::mainprogram()
>{
>   ifstream datafile ("c:\\abcde.txt"); //c:\abce.txt
>                                        //doesn't exists
>   int abc=0;
>   if (!datafile.is_open())  
>   {
>       MessageBox("The file was not opened.\n Please
>retry", "Error message", MB_OK | MB_ICONINFORMATION);

>       abc=1; //it is not called
>   }
>success = TRUE;
>if (abc==1) success = FALSE;
>}

>although abcde.txt doesn't exist, but datafile will
>create a new file and as a result the messagebox will not
>appear. is the yr m_FileNameFull similar
>to "c:\\abcde.txt" ?

>>-----Original Message-----
>>Justin,

>>Here is some more info about my application. Hopefully

>this will clear

>>up some things:

>>I have the following function which is supposed to call

>the

>>mainprogram() which contains among others the check

>whether the file to

>>be read exists and issues the message box in case that

>it does not.

>>void CDialog1Dlg::OnOK()
>>{
>>  extern bool success;  //success is false if an

>invalid file is selected

>>  mainprogram();  //Calls the function mainprogram.
>>                  //When this process ends, the

>estimator finishes.

>>  if(success == FALSE)
>>      return;            //if invalid file selected,

>allows user to

>>enter another one

>>CDialog::OnOK();
>>(then it has some other irrelevant code)

>>Following is my main program

>>void CDialog1Dlg::mainprogram()
>>{

>>  ifstream datafile (m_FileNameFull); //m_FileNameFull

>was read in the

>>Browse function

>>  int abc=0; //to check if the following 'if' loop is

>called

>>  if (!datafile.is_open())     //checks to see if file

>opens successfully

>>  {
>>      MessageBox("The file was not opened.\n Please

>retry", "Error

>>message", MB_OK | MB_ICONINFORMATION);

>>      abc=1; //it is not called
>>  }
>>extern bool success;
>>success = TRUE;
>>if (abc==1) success = FALSE; //returns FALSE and allows

>user to reenter

>>file (checked above in onOK() function)

>>Hope this helps.

>>Thanks for your interest,
>>Elias


>>>MessageBox need four parameters as below,

>>>MessageBox(0,"The file was not opened.\n Please
>>>   retry", "Error message", MB_OK |

>MB_ICONINFORMATION);

>>>I can not reproduce your problem by running the code as
>>>below,

>>>// CASE.cpp : Defines the entry point for the

>application.

>>>//

>>>#include "stdafx.h"
>>>#include "windows.h"

>>>int APIENTRY WinMain(HINSTANCE hInstance,
>>>                    HINSTANCE hPrevInstance,
>>>                    LPSTR     lpCmdLine,
>>>                    int       nCmdShow)
>>>{
>>>    int abc=0; //to check if the following 'if' loop
>>>is called
>>>   if (1)     //checks to see if file opens

>successfully

>>>   {  
>>>       MessageBox(0,"The file was not opened.\n Please
>>>retry", "Error message", MB_OK | MB_ICONINFORMATION);

>>>       abc=1; //it is not called
>>>   }
>>>    return 0;
>>>}

>>>can you give me more code about yr project? you can

>send


>>>thanks

>>>>-----Original Message-----
>>>>I have a dialog which among others asks for the name

>of

>>>a data file. If

>>>>this file does not exist, then a message box is

>>>displayed using the

>>>>following code:

>>>>int abc=0; //to check if the following 'if' loop is

>>>called

>>>>  if (!datafile.is_open())     //checks to see if

>file

>>>opens successfully

>>>>  {  
>>>>      MessageBox("The file was not opened.\n Please

>>>retry", "Error

>>>>message", MB_OK | MB_ICONINFORMATION);

>>>>      abc=1; //it is not called
>>>>  }

>>>>When I execute the program, and the message box is

>>>displayed, when I

>>>>click on OK, I get a runtime error and the application

>>>shuts down.

>>>>How can I fix this? What I want to do, is return back

>to

>>>my dialog and

>>>>make the necessary corrections.

>>>>Thank you in advance,
>>>>Elias

>>>>.



Mon, 21 Feb 2005 14:56:45 GMT  
 Problem with message box (causes runtime error)

Would this help?

OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.Flags = OFN_CREATEPROMPT; // if file doesn't exist, ask for create permission
or
ofn.Flags = OFN_FILEMUSTEXIST; // file must exist for "browse" window to close
GetOpenFileName(&ofn);


  Justin,

  The problem is that when I manually type a non-existent data file in the edit box, I get firstly the message box asking the user to retry, and when I click OK, I get "Runtime error", "Abnormal Program Termination."  Then it shuts down the program.

  What I need is basically to be able to go back in my dialog and be allowed to reenter a data file.

  Any ideas on that?



Tue, 22 Feb 2005 02:03:06 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Problem with message box (runtime error after I click OK)

2. free() causes runtime error with void* in a linked list

3. What are the various causes of a Visual C++ Runtime Library Error

4. Using strings in an object exported from a DLL causes runtime error

5. Modal dialog or message box causes exception

6. Message maps causes linker problems

7. User Control (from VB5 CCE) causing problems when used in dialog box

8. list box problem (redraw at runtime)

9. Microsoft Visual C++ Runtime Library Runtime Error When Browsing in IE6

10. Microsoft Visual C++ Runtime Library Runtime Error When Browsing in IE6

11. What error could cause this problem.

12. Stopping error message boxes when using CInternetSession

 

 
Powered by phpBB® Forum Software