String mfc-浮点到字符串,将其存储在文件中并从文件中读取

String mfc-浮点到字符串,将其存储在文件中并从文件中读取,string,file,mfc,output,String,File,Mfc,Output,我正在使用VisualStudio2010创建MFC应用程序。 我必须使用一个带有两个编辑控件的对话框,在编辑控件中键入的值必须添加并在屏幕上打印,如:“Addend1+Addend2=Result”。 现在,我使用_ttof()函数从字符串中获取浮点值,在我添加两个值之后,我应该使用哪个函数从浮点值中获取字符串? 完成后,我必须将其存储在文件中并读取。我是这样做的: void CseminarskiDoc::Serialize(CArchive& ar) { 是否有“Addend1+

我正在使用VisualStudio2010创建MFC应用程序。 我必须使用一个带有两个编辑控件的对话框,在编辑控件中键入的值必须添加并在屏幕上打印,如:“Addend1+Addend2=Result”。 现在,我使用_ttof()函数从字符串中获取浮点值,在我添加两个值之后,我应该使用哪个函数从浮点值中获取字符串? 完成后,我必须将其存储在文件中并读取。我是这样做的:

void CseminarskiDoc::Serialize(CArchive& ar)
{

是否有“Addend1+Addend2=Result”这样的输出

在ExView.cpp文件中,我是否必须添加一些行来执行输出,或者最后一个命令可以是function to do float->string

对不起,我的英语不好


谢谢:D

您可以使用如下代码将浮点转换为字符串:

CString s;
s.Format(_T("%f"), floatvariable);
它还可用于构建所需的输出字符串:

s.Format(_T("%f + %f = %f"), Addend1, Addend2, Result);

CArchive stuff不会在屏幕上显示任何内容,也不会连接字符串。事实上,CArchive不会创建普通文本文件,因此它可能根本不适合您在文件中所需的内容。

您这样做是错误的

不要在对话框中使用文本或CString变量。而是定义浮点或双精度的变量。我猜您使用的是浮点

// members of class CZBroj in your ZBroj.h

float m_fVar1;
float m_fVar2;

void CZBroj::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
   DDX_Text(pDX, IDC_EDIT1, m_fVar1); // if not IDC_EDIT1, change to correct ID
   DDX_Text(pDX, IDC_EDIT2, m_fVar2);
}

 void CseminarskiView::OnEditZbroj()
{
   CseminarskiDoc* pDoc = GetDocument();
   CZbroj dlg; // deklariranje dijaloga
   dlg.m_fVar1 = 3.4; //use whatever value you want to initialize with
   dlg.m_fVar2 = 1.414; // again, init with whatever you want, or be sure to init in constructor
   if (IDOK == dlg.DoModal())
   {
       // respond appropriately
       // dlg.m_fVar1,dlg.m_fVar2 will contain the edited values...magic of DDX_Text
   }
}
s.Format(_T("%f + %f = %f"), Addend1, Addend2, Result);
// members of class CZBroj in your ZBroj.h

float m_fVar1;
float m_fVar2;

void CZBroj::DoDataExchange(CDataExchange* pDX)
{
   CDialog::DoDataExchange(pDX);
   DDX_Text(pDX, IDC_EDIT1, m_fVar1); // if not IDC_EDIT1, change to correct ID
   DDX_Text(pDX, IDC_EDIT2, m_fVar2);
}

 void CseminarskiView::OnEditZbroj()
{
   CseminarskiDoc* pDoc = GetDocument();
   CZbroj dlg; // deklariranje dijaloga
   dlg.m_fVar1 = 3.4; //use whatever value you want to initialize with
   dlg.m_fVar2 = 1.414; // again, init with whatever you want, or be sure to init in constructor
   if (IDOK == dlg.DoModal())
   {
       // respond appropriately
       // dlg.m_fVar1,dlg.m_fVar2 will contain the edited values...magic of DDX_Text
   }
}