如何在C 中获取.CSV文件的一部分
How to get part of an .csv file in C++?
我是C 的新手。我只想在" .csv"文件上获取某个字段,而不是全部删除。我很确定,这一定很容易,但是我不知道该怎么做。这是我的代码获取所有" .csv"内容:
#include <iostream>
#include <fstream>
#include <string>
// #include "Patient.h"
using namespace std;
int main()
{
// CPatient patient;
ifstream file("C:/Users/Alex/Desktop/STAGE/test.csv");
if(file)
{
// the file did open well
string line;
while(getline(file, line, ';')) //Until we did not reach the end we read
{
cout << line << endl; //Console Result
}
}
else
{
cout << "ERROR: Could not open this file." << endl;
}
system("PAUSE");
return 0;
}
如果您可以使用boost
库,则boost::tokenizer
将提供您需要的功能。大多数名字,它正确处理包含逗号的字段值。以下是从链接页面复制的代码段:
// simple_example_2.cpp
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
int main(){
using namespace std;
using namespace boost;
string s = "Field 1,"putting quotes around fields, allows commas",Field 3";
tokenizer<escaped_list_separator<char> > tok(s);
for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin();
beg!=tok.end();
++beg)
{
cout << *beg << "n";
}
}
您可以将每个ligne
传递给tokenizer
并提取所需的字段。
尝试阅读整行,然后将它们分开:
int N = 5; // search the fifth field
char separator = ';';
while (std::getline(fichier, ligne)) {
// search for the Nth field
std::string::size_type pos = 0;
for (int i = 1; i < N; ++i)
pos = ligne.find_first_of(separator, pos) + 1;
std::string::size_type end = ligne.find_first_of(separator, pos);
// field is between [pos, end)
}
相关文章:
- 正在将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)