简体   繁体   English

C ++将参数3列csv文件中的逗号分隔的浮点数转换为3个独立的向量

[英]C++ transferring comma sepetated floats from argument 3-column csv file into 3 sepetate vectors

I have a csv file which is passed from command line and looks something like: 我有一个从命令行传递的csv文件,看起来像:

    2.26645, -0.258901, -213.274
    5.45054, 345.333, 73.1871
    7.18079, 25.156, 125.408
    ...

I want to extract these floats into 3 float vectors, one for each column. 我想将这些浮点数提取为3个浮点向量,每列一个。 I have only been able to extract ALL floats in just one vector, 我只能在一个向量中提取所有浮点数,

ifstream input_file;
input_file.open(argv[1],ios::out);

string line;
string value;

while(getline(input_file,line)){
    istringstream ss(line);
    while(getline(ss,value,',')){


    }


}

The issue is that as stated before, I want 3 vectors one for each column instead. 问题是,如前所述,我希望每列3个向量。

The first problem is because you put the file name into the string stream instead of the file contents. 第一个问题是因为您将文件名而不是文件内容放入字符串流。 Open a proper input file stream to read the data from the file. 打开适当的输入文件流以从文件中读取数据。


After you solved the above problem, the second problem could be solved by reading one line at a time from the file and put into an input string stream . 解决上述问题之后,可以通过一次从文件中读取一行并将其放入输入字符串流来解决第二个问题。 Then parse the input from that stream. 然后解析该流中的输入。

That parsing can be done similarly to what you do know skipping over space and the comma. 可以像您所知道的跳过空格和逗号一样来完成该解析。 Or you can use std::getline three times to get each value ( std::getline supports custom "field" separators) and then convert the strings to floating point numbers . 或者,您可以使用std::getline三次以获取每个值( std::getline支持自定义“字段”分隔符),然后将字符串转换为浮点数 Put each number in its own vector. 将每个数字放在自己的向量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM