Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC VS2010
Hello! I have a problem with the following code, it gives me an error to CFileDialog part:

error C2664: 'CFileDialog::CFileDialog(BOOL,LPCTSTR,LPCTSTR,DWORD,LPCTSTR,CWnd *,DWORD,BOOL)' : cannot convert parameter 2 from 'const char [5]' to 'LPCTSTR'
 
void CROSH1Dlg::OnBnClickedSave()
{
    UpdateData();
    CStudentGrade StdGrades;
    if( this->m_StudentName == "" )
        return;
    //strcpy (StdGrades.StudentName, this->m_StudentName);
    StdGrades.StudentName = this->m_StudentName;
    StdGrades.SchoolYear1 = this->m_SchoolYear1;
    StdGrades.SchoolYear2 = this->m_SchoolYear2;
    StdGrades.English     = this->m_English;
    StdGrades.History     = this->m_History;
    StdGrades.Economics   = this->m_Economics;
    StdGrades.Language2   = this->m_2ndLanguage;
    StdGrades.Geography   = this->m_Geography;
    StdGrades.Arts        = this->m_Arts;
    StdGrades.Math        = this->m_Math;
    StdGrades.Science     = this->m_Science;
    StdGrades.PhysEduc    = this->m_PhysEduc;
    StdGrades.Total       = this->m_Total;
    StdGrades.Average     = this->m_Average;
 
<pre>
CString strFilename = this-&gt;m_StudentName;
CFile fleGrades;
char strFilter[] = { "Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" };
CFileDialog dlgFile(FALSE, ".dnt", strFilename, 0, strFilter);
 
if( dlgFile.DoModal() == IDOK )
{
    // Save the record in binary format
    ofstream stmGrades(dlgFile.GetFileName(), ios::binary);
    stmGrades.write((char *)&amp;StdGrades, sizeof(StdGrades));
 
    // Reset the dialog box in case the user wants to enter another record
    m_StudentName = "";
    m_SchoolYear1 = 2000;
    m_SchoolYear2 = 2001;
    m_English     = 0.00;
    m_History     = 0.00;
    m_Economics   = 0.00;
    m_2ndLanguage = 0.00;
    m_Geography   = 0.00;
    m_Arts        = 0.00;
    m_Math        = 0.00;
    m_Science     = 0.00;
    m_PhysEduc    = 0.00;
    m_Total       = 0.00;
    m_Average     = 0.00;
}
 
UpdateData(FALSE);

}

Posted 17-Apr-11 7:16am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

When you are using strings use them like this
_T("Your String..")
So to solve your problem use this code
CFileDialog dlgFile(FALSE,_T(".dnt"), strFilename, 0, strFilter);
and use..
TCHAR strFilter[] = _T("Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" );
  Permalink  
Comments
Hans Dietrich at 17-Apr-11 12:49pm
   
The OP didn't say he got an error on this line:
if( this->m_StudentName == "" )
so he must not be using Unicode.
mr.abzadeh at 17-Apr-11 14:20pm
   
I think he uses unicode and solution 2 is True.
for m_StudentName to be unicode, It must be declared as
_TCHAR m_StudentName[some number];
or
wchar_t m_StudentName[some number];
Hans Dietrich at 17-Apr-11 22:48pm
   
If that's true, then why didn't OP report an error on that line?
mr.abzadeh at 18-Apr-11 9:04am
   
StudentName is declared as
char m_StudentName[some number];
This declares m_StudentName as MBCS, even if using unicode chars, and The instruction
if( this->m_StudentName == "" )
compares two char pointers
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
    CString strDefExt = ".dnt";
    CFileDialog dlgFile(FALSE, strDefExt, strFilename, 0, strFilter);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

It's working, thank you for your help, however I have another problem with the open button, two errors:

error C2664: 'void ATL::CStringT<basetype,stringtraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [12]' to 'const wchar_t *'
 
caption.Format("Student: %s", name);

error C2664: 'void ATL::CStringT<basetype,stringtraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'
 
this->m_StudentName.Format("%s", name);

Full code here, I try to make a open button:
 
void CROSH1Dlg::OnBnClickedOpen()
{
    UpdateData();
 
<pre>
CStudentGrade StdGrades;
CFile fleGrades;
 
//TCHAR strFilter[] = _T("Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" );
//CFileDialog dlgFile(FALSE,_T(".dnt"), strFilename, 0, strFilter);

TCHAR strFilter[] = _T("Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||");
CFileDialog dlgFile (TRUE,_T(".dnt"), NULL, 0, strFilter);
 
if( dlgFile.DoModal() == IDOK )
{
    // Open a record in binary format
    ifstream stmGrades(dlgFile.GetFileName(), ios::binary);
    stmGrades.read((char *)&amp;StdGrades, sizeof(StdGrades));
 
    // Change the caption of the dialog box to reflect the current grade
    char name[40];
    //strcpy(name, StdGrades.StudentName);
     StdGrades.StudentName=name;
    CString caption;
    caption.Format("Student: %s", name);
    this-&gt;SetWindowText(caption);
 
    // Display the record in the dialog box
    this-&gt;m_StudentName.Format("%s", name);
    this-&gt;m_SchoolYear1 = StdGrades.SchoolYear1;
    this-&gt;m_SchoolYear2 = StdGrades.SchoolYear2;
    this-&gt;m_English     = StdGrades.English;
    this-&gt;m_History     = StdGrades.History;
    this-&gt;m_Economics   = StdGrades.Economics;
    this-&gt;m_2ndLanguage = StdGrades.Language2;
    this-&gt;m_Geography   = StdGrades.Geography;
    this-&gt;m_Arts        = StdGrades.Arts;
    this-&gt;m_Math        = StdGrades.Math;
    this-&gt;m_Science     = StdGrades.Science;
    this-&gt;m_PhysEduc    = StdGrades.PhysEduc;
    this-&gt;m_Total       = StdGrades.Total;
    this-&gt;m_Average     = StdGrades.Average;
}
 
UpdateData(FALSE);

}

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web02 | 2.8.150331.1 | Last Updated 17 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100