如何通过CFile的函数向文件写入一个double型的数据(这个数据是一个double的变量)?  我知道的是将double型转换为Cstring类,然后再写入,但我不知怎么转换,请朋友们帮帮忙!!!

解决方案 »

  1.   

    char szBuffer[100];
    wsprintf(szBuffer,"%f",dblValue);
      

  2.   

    CString s;
    double d=1.02365;
    s.format(_T("%f"),d);另,发错板块了!
      

  3.   

    二楼华仔的代码当然没有错,他只是告诉你,你可以将 DOUBLE 转换为字符串。
    至于你还要通过CFile写入到文件中,那你就自己写几行代码,先调用CFile的Open函数打开文件,然后再调用CFile的Write函数,将刚才华仔转换后的字符串写入到文件中,然后再调用CFile的Close函数关闭文件即可。
      

  4.   

    CString mystr;
    double num=12.34;
    mystr.Format("%f",num);
      

  5.   

    你是不是想要完整的代码啊?
    如果是的,我给你写几句:
    CFile hFile;
    double fValue = 3.1415926;
    CString strFilePath = _T("C:\\Test.txt");
    if (!hFile.Open(strFilePath, CFile::modeWrite|CFile::modeCreate))
    {
         printf("Create File Failed, FileName = %s\n", strFilePath);
    }
    else
    {
         CString strValue = _T("");
         strValue.Format("%f", dValue);
         hFile.Write(strValue, strValue.GetLenth());
    } hFile.Close();
      

  6.   

    呵呵~略为修改一下:TCHAR szBuffer[100];
    _stprintf_s(szBuffer,_T("%f"),dblValue);
      

  7.   


     CString strValue = _T("");
      strValue.Format("%f", dValue);
      hFile.Write(strValue, strValue.GetLenth());
    问题基本解决,但这个_T是什么意思? 还有几是怎么控制 浮点数的小数点位数,按照你这样输出默认的6位,
    请说一下。。
      

  8.   


    CString strText(_T(""));
    double num = 12.3456789;
    strText.Format(_T("%lf"), num);
    try
    {
    CStdioFile file;
    file.Open(_T("F:\\11.txt"), CFile::modeCreate|CFile::modeWrite);
    file.WriteString(strText);
    file.Close();
    }
    catch (CFileException* e)
    {
    e->ReportError();
    e->Delete();
    }
      

  9.   

    以二进制文件的形式用CFile打开文件,然后直接写入double变量