Solved

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

Posted on 2007-04-01
3
900 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ error cannot convert from std::string to const char * 6 305
Which Linux flavors will this run on? 6 102
Issues with C++ Class 19 107
Finding Divisors 5 37
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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