簡體   English   中英

使用從 .csv 文件中提取的數據(stod、strtod、atof)在 C++ 中從字符串轉換為雙精度

[英]Conversion from string to double in C++ with data extracted from .csv file (stod, strtod, atof)

當我嘗試使用函數 stod、strtod 和 atof 將從 csv 文件中提取的字符串值轉換為兩倍時,我遇到了問題。 我只得到值的 integer 部分。

我使用 catkin build 編譯,它用於構建 ROS 中使用的包。 我要轉換的值是這個 csv 文件中第二行的 8 個值:

"1","2","3","4","0.0509105","-0.0101653","-0.105985","-0.0534463","Joint Positions"

csv 檔案

我將它們提取到一個字符串向量中:

代碼:

ifstream myfile;
myfile.open(path+robot_state_name+".csv");

string str_value;
getline(myfile, str_value); //The first line will be overwriten
vector<string> positions_str;

cout<<"Positions in string format from csv file : "<<endl;
for(int i = 0; i<=nb_of_variables-1; ++i){
    getline(myfile, str_value,',');
    positions_str.push_back(str_value);
    cout<<i<<" : "<<positions_str[i]<<endl;
}

控制台 output:

Positions in string format from csv file : 
0 : 1
1 : 2
2 : 3
3 : 4
4 : "0.0509105"
5 : "-0.0101653"
6 : "-0.105985"
7 : "-0.0534463"

然后我使用 std::stod() 將數據轉換為 double。 它適用於前 4 個數字,但隨后會引發錯誤。 並不是 stod() 承認的小數點分隔符是重點,如 csv 文件中那樣。

代碼:

cout<<"Position in double format using stod() : "<<endl;
for(int i = 0; i<=nb_of_variables-1; ++i){
    cout<<i<<" : "<<stod(positions_str[i])<<endl;
}

控制台 output:

Position in double format using stod() : 
0 : 1
1 : 2
2 : 3
3 : 4
terminate called after throwing an instance of 'std::invalid_argument'
what():  stod
Abandon (core dumped)

似乎 stod() 不能抓住要點……我還嘗試了 strtod() 和 atof(),它們為最后 4 個值返回 0。

但是當我自己輸入值時,它起作用了。

代碼:

cout<<"Results of these 3 functions when I type the string values by hand : "<<endl;
positions_str = {"1", "2", "3", "4", "0.0509105", "-0.0101653", "-0.105985", "-0.0534463"};
cout<<"stod() :  strtod() :  atof() :  "<<endl;
char* end_strtod;
for(int i = 0; i<=nb_of_variables-1; ++i){
    cout<<stod(positions_str[i])<<" ; "<<strtod(positions_str[i].c_str(), &end)<<" ; "<<atof(positions_str[i].c_str())<<endl;
}

控制台 output:

Results of these 3 functions when I type the string values by hand :
stod() :  strtod() :  atof() : 
1 ; 1 ; 1
2 ; 2 ; 2
3 ; 3 ; 3
4 ; 4 ; 4
0.0509105 ; 0.0509105 ; 0.0509105
-0.0101653 ; -0.0101653 ; -0.0101653
-0.105985 ; -0.105985 ; -0.105985
-0.0534463 ; -0.0534463 ; -0.0534463

我放了我為 strtod 和 atof 編寫的代碼,以及 function 定義。

停止strtodatof _

代碼:

cout<<"Position in double format using strtod() : "<<endl;
char* end;
for(int i = 0; i<=nb_of_variables-1; ++i){
    cout<<i<<" : "<<strtod(positions_str[i].c_str(), &end)<<endl;
}

cout<<"Position in double format using atof() : "<<endl;
for(int i = 0; i<=nb_of_variables-1; ++i){
    cout<<i<<" : "<<atof(positions_str[i].c_str())<<endl;
}

控制台 Output:

Position in double format using strtod() : 
0 : 1
1 : 2
2 : 3
3 : 4
4 : 0
5 : 0
6 : 0
7 : 0
Position in double format using atof() :
0 : 1
1 : 2
2 : 3
3 : 4
4 : 0
5 : 0
6 : 0
7 : 0

非常感謝那些花時間回答的人。

在調用 std::stod 之前,您需要從字符串的開頭和結尾刪除"

將您的轉換代碼更改為:

for (auto &s : positions_str)
    s.erase(remove(s.begin(), s.end(), '\"'), s.end()); // this will remove double quotes from the string if present

cout << "Position in double format using stod() : " << endl;

for(int i = 0; i < positions_str.size(); ++i)
    cout << i << " : " << stod(positions_str[i]) << endl;

那么,您應該在使用文本編輯器打開csv文件后發布了它的內容。

結構是這樣的:

1,2,3,4,"0.0509105","-0.0101653","-0.105985","-0.0534463","Joint Positions"

前四列為數字類型,其他一般為字符串。

如果前四個帶有" ,那么您將不會獲得索引0 - 3的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM