andy06
asked on
Saving array values with CFileDialog
Hi Experts,
I have a MFC dialog (VS2008 pro) application which includes a button to save values.
By clicking on the button I should create a file (.txt) and save the array values (double) in it.This
.txt file should be used by another program for ploting.
This is what I'm trying to do:
void CTestDlg::OnBnClickedSaveV alues()
{
CFileDialog MyDialog ( FALSE, _T("txt"),NULL, OFN_HIDEREADONLY |OFN_CREATEPROMPT,_T ("Text Files(*.txt)|*.txt|"));
CString TheString;
MyDialog.DoModal();
TheString = MyDialog.GetPathName();
FILE *MyFile;
int P=3, Loop=0;
double a_old[3]={2.584,3.259,4.42 3},a[3],de lta_a[3]={ -0.142,0.0 054,0.5};
double Alpha=-0.658403, Rho=-0.2;
//Open the file
MyFile= fopen(TheString,_T("a+"));
while(Loop<3)
{
for(int j=0;j<P;j++)
{
a[j]=a_old[j] + Rho*Alpha*delta_a[j];
//Here are the values of the array to be saved
if( MyDialog.DoModal() == IDOK )
{
fprintf(MyFile, "Loop %d\n", Loop);
fprintf(MyFile, "%.7f\n", a[j]);
}
}
Loop++;
}
fclose(MyFile);
}
And here is the error message:
error C2664: 'fopen' : cannot convert parameter 1 from 'CString' to 'const char *'
Andy
I have a MFC dialog (VS2008 pro) application which includes a button to save values.
By clicking on the button I should create a file (.txt) and save the array values (double) in it.This
.txt file should be used by another program for ploting.
This is what I'm trying to do:
void CTestDlg::OnBnClickedSaveV
{
CFileDialog MyDialog ( FALSE, _T("txt"),NULL, OFN_HIDEREADONLY |OFN_CREATEPROMPT,_T ("Text Files(*.txt)|*.txt|"));
CString TheString;
MyDialog.DoModal();
TheString = MyDialog.GetPathName();
FILE *MyFile;
int P=3, Loop=0;
double a_old[3]={2.584,3.259,4.42
double Alpha=-0.658403, Rho=-0.2;
//Open the file
MyFile= fopen(TheString,_T("a+"));
while(Loop<3)
{
for(int j=0;j<P;j++)
{
a[j]=a_old[j] + Rho*Alpha*delta_a[j];
//Here are the values of the array to be saved
if( MyDialog.DoModal() == IDOK )
{
fprintf(MyFile, "Loop %d\n", Loop);
fprintf(MyFile, "%.7f\n", a[j]);
}
}
Loop++;
}
fclose(MyFile);
}
And here is the error message:
error C2664: 'fopen' : cannot convert parameter 1 from 'CString' to 'const char *'
Andy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>Change the project's character set from "unicode" to "multibyte"
Is there another option as to change the character set from "unicode" to "multibyte"?
>Should if( MyDialog.DoModal() == IDOK ) really be inside the loop ?
No...
Is there another option as to change the character set from "unicode" to "multibyte"?
>Should if( MyDialog.DoModal() == IDOK ) really be inside the loop ?
No...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Is there another option as to change the character set from "unicode" to "multibyte"?
As I suggested use the MFC supplied 'FILE' wrapper classes - they cope with UNICODE file names.
>Should if( MyDialog.DoModal() == IDOK ) really be inside the loop ? No...
:-) Thought it looked odd.
As I suggested use the MFC supplied 'FILE' wrapper classes - they cope with UNICODE file names.
>Should if( MyDialog.DoModal() == IDOK ) really be inside the loop ? No...
:-) Thought it looked odd.
Should if( MyDialog.DoModal() == IDOK ) really be inside the loop ?