在C 中读取CSV文件
read csv file in c++
i有一个.csv文件,该文件仅包含两个带有人名称和年龄的列。看起来像:
Name Age
Peter 16
George 15.5
Daniel 18.5
我只想在双打矢量中收集人们的年龄。所以我想拥有诸如vect = {16,15.5,18.5}的东西。
仅使用标准库时我该如何实现?
非常感谢
@bugsfree谢谢您的脚本,但似乎对我不起作用。
这是我最终做到的(如果有人感兴趣的话...)
ifstream infile("myfile.csv");
vector<string> classData;
vector<double> ages;
std::string line;
while (getline(infile, line,'n'))
{
classData.push_back(line); //Get each line of the file as a string
}
int s = classData.size();
for (unsigned int i=1; i<s; ++i){
std::size_t pos = classData[i].find(","); // position of the end of the name of each one in the respective string
ages[i-1] = std::stod(classData[i].substr(pos+1,classData[i].size())); // convert string age to a double
}
您可以做这样的事情:
#include <sstream>
#include <string>
#include <fstream>
ifstream infile( "yourfile.csv" );
std::vector<double> ages;
while (infile)
{
std::string line;
if (!std::getline( infile, line,' ' )) break;
std::istringstream iss(line);
string name;
double age;
if (!(iss >> name >> age)) { break; }
ages.push_back(age);
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 如何选择在 csv 文件中输出的位置
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 无法读取C++中的 CSV 文件
- 流:CSV 文件中的换行符
- 如何正确格式化csv文件?
- 正在分析CSV文件-C++
- 如何获取组件和搜索 CSV 文件
- C++ 中的 CSV 文件管理(特别是设置标头和每个标头将分别具有的值)
- 无法逐行读取.csv文件
- 非常快速地阅读 CSV 文件
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- CSV文件之间的差异,使用getline()产生不同的结果
- 将 CSV 文件中的双倍值C++到矢量中
- C++ 将 CSV 文件的列写入向量
- 使用getline读取.csv文件时出现问题
- CSV文件未从缓冲区获取数据
- 错误的值是使用c++从CSV文件数据写入数组
- 将csv文件加载到MySQL C++中
- 读取C++中的大型CSV文件(~4GB)