Click here to Skip to main content
15,885,680 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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;

	CString strFilename = this->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 *)&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

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 (*.*)|*.*||" );
 
Share this answer
 
Comments
Hans Dietrich 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 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 17-Apr-11 22:48pm    
If that's true, then why didn't OP report an error on that line?
mr.abzadeh 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
Try this:
CString strDefExt = ".dnt";
CFileDialog dlgFile(FALSE, strDefExt, strFilename, 0, strFilter);
 
Share this answer
 
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();

	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 *)&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->SetWindowText(caption);

		// Display the record in the dialog box
		this->m_StudentName.Format("%s", name);
		this->m_SchoolYear1 = StdGrades.SchoolYear1;
		this->m_SchoolYear2 = StdGrades.SchoolYear2;
		this->m_English     = StdGrades.English;
		this->m_History     = StdGrades.History;
		this->m_Economics   = StdGrades.Economics;
		this->m_2ndLanguage = StdGrades.Language2;
		this->m_Geography   = StdGrades.Geography;
		this->m_Arts        = StdGrades.Arts;
		this->m_Math        = StdGrades.Math;
		this->m_Science     = StdGrades.Science;
		this->m_PhysEduc    = StdGrades.PhysEduc;
		this->m_Total       = StdGrades.Total;
		this->m_Average     = StdGrades.Average;
	}

	UpdateData(FALSE);
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900