我怎样才能得到字符串数字?

时间:2016-11-07 01:54:10

标签: c++

我正在进行数据收集,并试图扫描标题并使用“sscanf”将其转换为实数,但它并没有像我预期的那样给出我的数字,这是写的“拆分函数”,正在帮我拆分字符串向量:

#include <iostream>   // std::cout
#include <string>     // std::string, std::stod


int main() {
   string fn = "ch-683-mhz-8000.0-ksps-2016-06-20-17.24.19-utc.dat";

   vector<string> v;
   int f0, fs;
   split(fn, '-', v);

   for (int i = 0; i < v.size(); ++i) {
      cout << i << "   " << v[i] << '\n';
   }
   for(unsigned i=1; i < v.size(); i++){
       if(v[i] == "mhz"){
          std::sscanf(v[i-1], &f0);
          int ret = sscanf(v[i-1], &f0);
          cout << v[i-1] ;
       }
   }
   return 0;
}

“向量v”会给我结果: 683 兆赫 8000.0 ksps的 2016 06 20 17.24.19 utc.dat

我希望将683和8000转换为实数,但我收到错误,即使我试图从论坛进行一些搜索:

Description Resource    Path    Location    Type
'resize' is ambiguous '
Candidates are:
void resize(unsigned long int, std::complex<float>)
'   fft.hpp /test2/src  line 186    Semantic Error
recipe for target 'src/test2.o' failed  subdir.mk   /test2/Debug/src    line 18 C/C++ Problem
'resize' is ambiguous '
Candidates are:
void resize(unsigned long int, std::complex<float>)
'   fft.hpp /test2/src  line 251    Semantic Error
cannot convert ‘std::__cxx11::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘int sscanf(const char*, const char*, ...)’  test2.cpp   /test2/src  line 495    C/C++ Problem
cannot convert ‘std::__cxx11::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘int sscanf(const char*, const char*, ...)’  test2.cpp   /test2/src  line 494    C/C++ Problem
Invalid arguments '
Candidates are:
int sscanf(const char *, const char *, ...)
'   test2.cpp   /test2/src  line 494    Semantic Error
Invalid arguments '
Candidates are:
int sscanf(const char *, const char *, ...)
'   test2.cpp   /test2/src  line 495    Semantic Error
make: *** [src/test2.o] Error 1 test2           C/C++ Problem

有人可以帮忙吗?

非常感谢你。

1 个答案:

答案 0 :(得分:0)

int main()
{
    string fn = "ch-683-mhz-8000.0-ksps-2016-06-20-17.24.19-utc.dat";
    string szt1;

    for (int i = 0; i < 15; i++)
    {
        szt1 = szt1 + fn[i];
    }


    int n1;
    int n2;
    string szn1;
    string szn2;

    for (int i = 3; i < szt1.length(); i++)
    {
        if (i < 6)
        {
            szn1 = szn1 + szt1[i];
        }
    }

    for (int i = 11; i < szt1.length(); i++)
    {
            szn2 = szn2 + szt1[i];
    }

    n1 = atoi(szn1.c_str());
    n2 = atoi(szn2.c_str());

    cout << n1 << endl;
    cout << n2 << endl;

    return 0;

}