Solved

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

Posted on 2007-04-01
3
875 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks! You are both right. I also tried
temp=fx.GetFilePath();
and it works too.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

763 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

11 Experts available now in Live!

Get 1:1 Help Now