PDOI: Cancel button in CFileDialog - Programmer

This is a discussion on PDOI: Cancel button in CFileDialog - Programmer ; PDOI stands for public display of ignorance. I use Do.Modal on a CFileDialog class to get a file. I use GetFilename, then later I use that name with an fopen command (reading a file the way I learned to do ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: PDOI: Cancel button in CFileDialog

  1. PDOI: Cancel button in CFileDialog

    PDOI stands for public display of ignorance.

    I use Do.Modal on a CFileDialog class to get a file. I use
    GetFilename, then later I use that name with an fopen command (reading
    a file the way I learned to do in C.)

    Everything works the way I want it to when I read the file that I
    want, but when I click on the "Cancel" button in a CFileDialog, the
    program crashes. I tried to do things like,

    m_filename=myCFDialog.GetFileName();
    if (m_filename == NULL) return;

    but the program still crashes. Any ideas what to code so that I can
    cancel out of a file read dialog? Redneckspeak is preferred. Thanks in
    advance, FIF

  2. Re: Cancel button in CFileDialog

    Check the value you get back from DoModal();

    ie:

    if (dlg.DoModal == IDOK) {
    // get filename
    // open file.
    }

    --
    -------------------------------------------------------------------------
    Free software - Baxter Codeworks www.baxcode.com
    -------------------------------------------------------------------------


    "Faith in Fisix" wrote in message
    news:974f981e.0311121110.41fdbf21@posting.google.c om...
    > PDOI stands for public display of ignorance.
    >
    > I use Do.Modal on a CFileDialog class to get a file. I use
    > GetFilename, then later I use that name with an fopen command (reading
    > a file the way I learned to do in C.)
    >
    > Everything works the way I want it to when I read the file that I
    > want, but when I click on the "Cancel" button in a CFileDialog, the
    > program crashes. I tried to do things like,
    >
    > m_filename=myCFDialog.GetFileName();
    > if (m_filename == NULL) return;
    >
    > but the program still crashes. Any ideas what to code so that I can
    > cancel out of a file read dialog? Redneckspeak is preferred. Thanks in
    > advance, FIF




  3. Re: PDOI: Cancel button in CFileDialog

    Hi,
    I think you proplem comes of your confusion about your misuse of the
    CString type. GetFileName returns a CString. What you're doing is
    testing NULL is returned.The confusion comes from thinking that an
    empty string (ie: "") has a NULL address. "" has a non NULL address
    because it's equal to '\0', wich is a valid char. So remember that
    GetFileName will never return NULL, since it returns a CString and not
    a CString *.

    Here is the correct way to do it, using the IsEmpty() method, with
    m_filename being a CString :

    m_filename=myCFDialog.GetFileName();
    if (m_filename.IsEmpty()) return;

    Regards,

    Luis MENINA

    phychobm@mindspring.com (Faith in Fisix)
    wrote in message news:<974f981e.0311121110.41fdbf21@posting.google.com>...
    > PDOI stands for public display of ignorance.
    >
    > I use Do.Modal on a CFileDialog class to get a file. I use
    > GetFilename, then later I use that name with an fopen command (reading
    > a file the way I learned to do in C.)
    >
    > Everything works the way I want it to when I read the file that I
    > want, but when I click on the "Cancel" button in a CFileDialog, the
    > program crashes. I tried to do things like,
    >
    > m_filename=myCFDialog.GetFileName();
    > if (m_filename == NULL) return;
    >
    > but the program still crashes. Any ideas what to code so that I can
    > cancel out of a file read dialog? Redneckspeak is preferred. Thanks in
    > advance, FIF


  4. Re: PDOI: Cancel button in CFileDialog

    Hi,

    You can try to not execute your code.

    When you call DoModal() the function returns a modal code.
    On CFileDialog (and other system dialogs as well) DoModal()
    returns IDCANCEL when you either hit ESC or click CANCEL
    or close the dialog withthe cross button.

    So your code should be

    if (myCFDialog.DoModal() != IDCANCEL)
    {
    m_filename = myCFDialog.GetPathName();
    // ... do your stuff
    }

    Also remember that GetFileName() does give you the file name
    without the file path. GetFilePath() is the correct method if you
    want this information.

    --
    Emmanuel Deloget
    i-magus S.A.

    "Luis Menina" wrote in message
    news:d610bbcc.0311122353.4a7a5898@posting.google.c om...
    > Hi,
    > I think you proplem comes of your confusion about your misuse of the
    > CString type. GetFileName returns a CString. What you're doing is
    > testing NULL is returned.The confusion comes from thinking that an
    > empty string (ie: "") has a NULL address. "" has a non NULL address
    > because it's equal to '\0', wich is a valid char. So remember that
    > GetFileName will never return NULL, since it returns a CString and not
    > a CString *.
    >
    > Here is the correct way to do it, using the IsEmpty() method, with
    > m_filename being a CString :
    >
    > m_filename=myCFDialog.GetFileName();
    > if (m_filename.IsEmpty()) return;
    >
    > Regards,
    >
    > Luis MENINA
    >
    > phychobm@mindspring.com (Faith in Fisix)
    > wrote in message news:<974f981e.0311121110.41fdbf21@posting.google.com>...
    > > PDOI stands for public display of ignorance.
    > >
    > > I use Do.Modal on a CFileDialog class to get a file. I use
    > > GetFilename, then later I use that name with an fopen command (reading
    > > a file the way I learned to do in C.)
    > >
    > > Everything works the way I want it to when I read the file that I
    > > want, but when I click on the "Cancel" button in a CFileDialog, the
    > > program crashes. I tried to do things like,
    > >
    > > m_filename=myCFDialog.GetFileName();
    > > if (m_filename == NULL) return;
    > >
    > > but the program still crashes. Any ideas what to code so that I can
    > > cancel out of a file read dialog? Redneckspeak is preferred. Thanks in
    > > advance, FIF




+ Reply to Thread