Solved

why did "FileDlg.GetFolderPath();" fail?

Posted on 2007-04-01
3
886 Views
Last Modified: 2010-05-18
The following codes compiles and links fine. But when I selected the file and tried to get the folder of the file, it shows the "Debug Assertion Failed!" message box from Microsoft Visual C++ Debug Library. What was wrong? My goal is to get the path and the name of the file selected. "FileDlg.GetFileName(); " can only get the name of the file. Thanks!

void CGrindGameDlg::OnBnClickedButton3()
{
      // TODO: Add your control notification handler code here
      CStdioFile fx;
      CString temp;

      CFileDialog FileDlg(TRUE, _T(".txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Points files (*.txt)|*.txt|All Files (*.*)|*.*|" ), NULL, 0);
      
      if( FileDlg.DoModal() == IDOK )
      {
            if( fx.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
                  return;
            
            temp=FileDlg.GetFolderPath();  // this line fails
            GetDlgItem(IDC_STATIC_x)->SetWindowText(temp);
      }
      else
            return;

      fx.Close();

}
0
Comment
Question by:applebibo
3 Comments
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 55 total points
ID: 18834604
According to MS:
The dialog box must have been created with the OFN_EXPLORER style; otherwise, the method will fail with an assertion.
You can call this method only while the dialog box is being displayed. After the dialog box has been closed, this function will no longer work, and the method will fail with an assertion.

Best Regards,
DeepuAbrahamK

0
 
LVL 3

Accepted Solution

by:
ieciep earned 70 total points
ID: 18834609
hi,

http://msdn2.microsoft.com/en-us/library/xz5y6xhy(VS.80).aspx

It says:

After the dialog box has been closed, this function will no longer work, and the method will fail with an assertion.

Actually when you press OK button dialog box is no longer there, and the method call fails.

you can achieve complete folder path by another way.

Try this:
--------

void CGrindGameDlg::OnBnClickedButton3()
{
      // TODO: Add your control notification handler code here
      CStdioFile fx;
      CString temp;

      CFileDialog FileDlg(TRUE, _T(".txt"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Points files (*.txt)|*.txt|All Files (*.*)|*.*|" ), NULL, 0);
     
      if( FileDlg.DoModal() == IDOK )
      {
            if( fx.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
                  return;
           
            temp=FileDlg.GetPathName();  // this line fails
            int charPosition=temp.ReverseFind('\\');
            CString filePath=temp.Left((charPosition+1));
            MessageBox(filePath);

            GetDlgItem(IDC_STATIC_x)->SetWindowText(temp);
      }
      else
            return;
      fx.Close();
}
0
 

Author Comment

by:applebibo
ID: 18834651
Thanks! You are both right. I also tried
temp=fx.GetFilePath();
and it works too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now