Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Fast, free, private online code repository / editor 7 106
C++ question 3 70
Microsoft C++ code failing in executable that worked 9 108
No module found pypyodbc, 3 32
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

860 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