do{
.......
x= ... ;  //x的计算式,且x的值是收敛的
.......
}while(x>0.00001);
那么怎样在对话框上按一下按钮,弹出要保存文件的路径的对话框,且输出包含x的所有的值的文本(因为x迭代了)?
我在对话框上加了一个按钮控件,且ID设为ID_FILE_SAVE_AS,其函数为OnFileSaveAs() 

解决方案 »

  1.   

    1,在你的按扭里用cfiledlg生成一个文件对话框。
    2,得到文件名。
    3,用cfile类打开文件,并把x的值写进去。
    另外,如果你只是想看看x的值的话,可以用控制台啊。
      

  2.   


    void C**Dlg::Calculate()
    {
    do{
    .......
    x= ... ;  //x的计算式,且x的值是收敛的
    .......
    }while(x>0.00001);
    UpdateData(false);
    }void C**Dlg::OnFileSaveAs() 
    { CFileDialog dlg(false,NULL,NULL,0,"数据文件(*.dat)|*.dat||"); 

    if(dlg.DoModal()==IDOK) 
    {
    CString str; 
    str =dlg.GetFileName(); 
    CFile file; 
    file.Open(str,CFile::modeCreate |CFile::modeWrite ); 
    str.Format("%f",x); 
    file.Write(str,str.GetLength()); 
    file.Close(); 

    }弹出了要保存文件的路径的对话框,可是Calculate()中的x怎样赋值给OnFileSaveAs()呢?
      

  3.   

    需要设置一个全局的变量存储x的值,否则x的生命周期只在Calculate()里面。
      

  4.   

    把x保存为成员变量m_x;这样你的OnFileSaveAs就可以访问m_x了
      

  5.   

           设置变量x为C**Dlg类的一个成员变量m_x,然后代码修改为:
    void C**Dlg::Calculate()
    {
    do{
    .......
    m_x= ... ;  //x的计算式,且x的值是收敛的
    .......
    }while(x>0.00001);
    UpdateData(false);
    }void C**Dlg::OnFileSaveAs() 
    {    CFileDialog dlg(false,NULL,NULL,0,"数据文件(*.dat)|*.dat||"); 
        
        if(dlg.DoModal()==IDOK) 
        {
            Calculate();
            CString str; 
            str =dlg.GetFileName(); 
            CFile file; 
            file.Open(str,CFile::modeCreate |CFile::modeWrite ); 
            str.Format("%f",m_x); 
            file.Write(str,str.GetLength()); 
            file.Close(); 
        } 
    }
      

  6.   

    全局变量我设了,关键是我想输出那个do while循环中m_x的所有的收敛的值。有没有办法呢?
      

  7.   


         输出那个do while循环中m_x的所有的收敛的值.
    void C**Dlg::Calculate(CString str)
    {
    do{
    .......
    m_x= ... ;  //x的计算式,且x的值是收敛的         CFile file; 
            file.Open(str,CFile::modeCreate |CFile::modeWrite ); 
            str.Format("%f",m_x); 
            file.Write(str,str.GetLength()); 
            file.Close(); .......
    }while(x>0.00001);
    UpdateData(false);
    }void C**Dlg::OnFileSaveAs() 
    {    CFileDialog dlg(false,NULL,NULL,0,"数据文件(*.dat)|*.dat||"); 
        
        if(dlg.DoModal()==IDOK) 
        {
           CString str; 
            str =dlg.GetFileName(); 
            Calculate(str);           } 
    }
      

  8.   

         错了,应该是:void C**Dlg::Calculate(CFile &file)
    {
    do{
    .......
    m_x= ... ;  //x的计算式,且x的值是收敛的 CString str; 
      str.Format("%f",m_x); 
            file.Write(str,str.GetLength()); 
    .......
    }while(x>0.00001);
    UpdateData(false);
    }void C**Dlg::OnFileSaveAs() 
    {    CFileDialog dlg(false,NULL,NULL,0,"数据文件(*.dat)|*.dat||"); 
        
        if(dlg.DoModal()==IDOK) 
        {
           CString str; 
            str =dlg.GetFileName(); 
        CFile file;
           file.Open(str,CFile::modeCreate |CFile::modeWrite );         Calculate(file);  file.Close(); 
               } 
    }