?
Solved

Saving array values with CFileDialog

Posted on 2009-02-20
6
Medium Priority
?
554 Views
Last Modified: 2013-12-14
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::OnBnClickedSaveValues()
{
 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.423},a[3],delta_a[3]={-0.142,0.0054,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
 
0
Comment
Question by:andy06
  • 3
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
alb66 earned 900 total points
ID: 23690127
Change the project's character set from "unicode" to "multibyte"
properties.JPG
0
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 600 total points
ID: 23690159
instead of FILE use the (MFC wrapper class) CStdioFile.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 23690166
ps.
Should       if( MyDialog.DoModal() == IDOK )  really be inside the loop ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:andy06
ID: 23690965
>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...
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 900 total points
ID: 23690982
Use _wfopen() (the unicode version) instead of fopen()
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 23691097
>>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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 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