如何在 c++ 中跳过读取 csv 文件的第一列
How to skip the first column reading csv file in c++
下面的代码是从csv文件中获取一些数据。但结果如下所示:
5,琼斯,123-123-1234,BCBS,GP1234,39,莎拉,断臂,3
6,史密斯,123-231-1234,UHC,G1234,47,弗朗辛,物理治疗,03/25/2015
9,亚当斯,123-123-4321,信诺,U1234,28,鲍勃,断臂,2
5,梵高,123-321-1234,BCBS,GP1235,37,安德里亚,肚子痛,3
10,锡器施密特,123-312-1234,UHC,G1112,42,Peter,监督正常第一次怀孕,03/26/2015
但是我想获取除第一列以外的数据(例如5,6,9,5,10(我该怎么做?你能给我一个想法吗?谢谢。
void Hospital::readRecordsFile()
{
fileName = "Waterbury Hospital patients records.csv";
ifstream file(fileName);
string value;
vector <string> getInform;
while(file.good())
{
getline(file, value);
getInform.push_back(value);
//getInform.erase(getInform.begin()+1);
}
for(int i=0;i<getInform.size();i++)
{
cout << getInform[i] << endl;
}
}
您可以在每行中找到第一个分隔符 (,(,然后删除它之前的所有字符:
getline(file, value);
const auto pos = value.find(',');
if(pos != string::npos)
value.erase(0, pos + 1);
如果您不确定 CSV 文件中是否使用了分隔符 (,(。您可能会忽略每行开头的所有数字:
getline(file, value);
const auto pos = value.find_first_not_of("0123456789");
if(pos != string::npos)
value.erase(0, pos + 1);
>std::istream::ignore
可用于忽略输入流中的某些文本。
从输入流中提取和丢弃字符,直到并包括
delim
。
file.ignore(std::numeric_limits<std::streamsize>::max(), ',');
并用getline
跟进以阅读该行的其余部分。
getline(file, value);
您应该用","拆分每一行,然后忽略第一部分。
只需解析字符串以丰富第一个","字符,然后使用该索引中的子字符串到结尾。
相关文章:
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 如何在C++中获取二维数组中最少的一列数?
- 在 C++ 中使用另一个头文件中的抽象类
- 如何读取没有数组或结构的 3 列文件
- 如何允许用户选择何时停止三列中的每一列的旋转
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- 读取目录中的多个文件以进行编辑和写入另一组文件
- 在犰狳立方体中添加一列 1 的有效方法
- 使用两个.cpp文件和一个头文件
- 在二维向量的每一列中查找最大值
- 只有一列适合其内容大小,而另一列则拉伸
- 如何将类对象作为对另一个头文件函数的引用传递
- 按列对CSV文件进行排序,并与C 中的另一列进行比较
- 读取数据文件并将每一列分配给各个数组
- 从文本文件中读取矩阵并在C++中使用一列的数字
- 如何在C++中仅将.CSV文件的一列解析、读取和存储到数组中
- 如何从文本文件中保存一列数据到c++中的数组
- 从CSV文件中读取一列数据
- 将文件中的每一列排序为一个单独的数组
- 从输入文件的最后一列决定一个例程