Solved

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

Posted on 2007-04-01
3
905 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

724 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