本文介绍了无法将字符串转换为float / double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在写一个类的代码,我从一个巨大的文件(约850行)的数据输入,用逗号分隔。我到目前为止是:
I'm writing a code for class where I take in input from a huge file (~850 lines) of data separated by commas. what I have so far is:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
struct Station
{
string StationID, StationName;
float Elevation;
double Latitude, Longitude;
int Date, MXPN, MaxTemp, MinTemp, ObsTime;
};
int main ()
{
vector <string> Data;
string DummyLine, TempLine;
int Size = 0;
ifstream InputFile;
InputFile.open("finalc++.csv");
getline(InputFile, DummyLine);
while (InputFile.good())
{
getline(InputFile, TempLine);
Size++;
stringstream ss (TempLine);
while (getline(ss, DummyLine, ',')) {
Data.push_back(DummyLine);
}
}
Station Entry[Size];
for (int i = 0; i <= Size; i++)
{
Entry[i].StationID = Data[i * 10];
Entry[i].StationName = Data[((i*10) + 1)];
Entry[i].Elevation = Data[((i*10) + 2)];
Entry[i].Latitude = Data[((i*10) + 3)];
Entry[i].Longitude = Data[((i*10) + 4)];
Entry[i].Date = Data[((i*10) + 5)];
Entry[i].MXPN = Data[((i*10) + 6)];
Entry[i].MaxTemp = Data[((i*10) + 7)];
Entry[i].MinTemp = Data[((i*10) + 8)];
Entry[i].ObsTime = Data[((i*10) + 9)];
}
return 0;
}
我试过使用 stof
做到这一点,但不能得到它附近的工作。
I tried using stof
to do this but could not get it anywhere near working. Any help would be appreciated.
推荐答案
托盘:
//this can be cause exeptions
Entry[i].Elevation = atof (Data[((i*10) + 2)].c_str());
注意:include cstdlib
NOTE: include cstdlib
这篇关于无法将字符串转换为float / double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!