【问题标题】:Extract a floating point number from a CString从 CString 中提取浮点数
【发布时间】:2012-08-13 14:00:59
【问题描述】:

我想从格式如下的 CString 中提取一个浮点数:(例如提取 22.760348)

Incidence_angle(inc)[deg]                 :22.760348

基本上我正在读取一个包含一些参数的纯文本文件,并且我想对这些值执行一些计算。我使用 CStdioFile 对象读取文件并使用 readString 方法提取每一行,如下所示:

CStdioFile result(global::resultFile,CFile::modeRead);
while( result.ReadString(tmp) )
            {
                if(tmp.Find(L"Incidence_angle(inc)[deg]") != -1)
                {
                    //extract value of theeta i here
                    // this is probably wrong
                    theeta_i = _tscanf(L"Incidence_angle(inc)[deg]  :%f",&theeta_i);
                }
            }

我尝试使用 scanf,因为我想不出任何其他方式。

如果这个问题看起来非常基本和愚蠢,我深表歉意,但我已经坚持了很长时间,希望得到一些帮助。

编辑:拿出我写的概念证明程序,造成混乱

【问题讨论】:

  • 你能出示theeta_i的声明吗?
  • @Andrey - float pixel_spacing=0, line_spacing=0, theeta_i=0;

标签: c++ mfc cstring


【解决方案1】:

假设tmpCString,那么正确的代码是

CStdioFile result(global::resultFile,CFile::modeRead);
while( result.ReadString(tmp) )
{
if (swscanf_s(tmp, L"Incidence_angle(inc)[deg]  :%f", &theeta_i) == 1)
    {
        // Use the float falue
    }
}

【讨论】:

  • 嗨@Andrey 我实现了这个。但它永远不会进入 if 大括号。条件始终评估为假。我在这里做错了吗?
  • CStdioFile 结果(global::resultFile,CFile::modeRead); while( result.ReadString(tmp) ) { if(tmp.Find(L"Incidence_angle(inc)[deg]") != -1) { //提取 theeta i 的值 if (wscanf_s(tmp, L"Incidence_angle( inc)[deg] :%f", &theeta_i) == 1) { // 使用浮点数 AfxMessageBox(L"Float",0,0); } } }
  • @eternalDreamer:它到达 sscanf 了吗?如果是,请确保"Incidence_angle(inc)[deg] :" 与输入流中的字符串完全匹配(包括大小写和空格数)。另外,那是 MBCS 还是 Unicode 应用程序?
  • 它到达了 sscanf。我从文本文件中完全复制了字符串。它是一个 Unicode 应用程序。如果我缺乏知识让您感到沮丧,我很抱歉。
  • @eternalDreamer 对不起,那你应该使用sscanf的Unicode等价物,它被称为swscanf_sAFAIR
【解决方案2】:

为什么不使用atof

示例取自链接:

   /* atof example: sine calculator */
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main ()
    {
      double n,m;
      double pi=3.1415926535;
      char szInput [256];
      printf ( "Enter degrees: " );
      gets ( szInput );
      n = atof ( szInput );
      m = sin (n*pi/180);
      printf ( "The sine of %f degrees is %f\n" , n, m );
      return 0;
    }

【讨论】:

  • 它不是控制台程序。如果我的概念验证程序引起了混乱,我很抱歉。
【解决方案3】:

为什么不完全采用 C++ 方式?

这只是一个提示:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
   double double_val=0.0;
   std::string dump("");
   std::string oneline("str 123.45 67.89 34.567"); //here I created a string containing floating point numbers
   std::istringstream iss(oneline);
   iss>>dump;//Discard the string stuff before the floating point numbers
   while ( iss >> double_val )
   {
      std::cout << "floating point number is = " << double_val << std::endl;
   }
   return 0;
}

如果你想按照你的说明使用,只使用 cstring,也可以试试strtod()来源:man -s 3 strtod

【讨论】:

    【解决方案4】:

    _tscanf() 返回分配的数量,而不是读取的值:

    theeta_i = _tscanf(L"Incidence_angle(inc)[deg]  :%f",&theeta_i); 
    

    所以如果float 被成功读取,theeta_i 将包含1(.0)。改为:

    if (1 == _tscanf(L"Incidence_angle(inc)[deg]  :%f",&theeta_i))
    {
        /* One float value successfully read. */
    }
    

    从缓冲区读取应该是_stscanf()_tscanf() 将等待来自标准输入的输入。

    【讨论】:

    • 感谢您的解释。我需要提取浮点值,所以我尝试使用这个:sscanf_s
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多