[英]How to read some columns from a CSV file with C++?
I would like to know how to read information from an Excel file (saved as .csv) for then printing it into another file, using C++. 我想知道如何从Excel文件(另存为.csv)中读取信息,然后使用C ++将其打印到另一个文件中。
The input file has the data separated into columns. 输入文件的数据分为几列。 The first line of each column is the label of the column. 每列的第一行是该列的标签。 Such as this: 如:
|Column1|Column2|Column3|Column4|Column5|Column6
|char1.1|char2.1| | | |char6.1
|char1.2|char2.2|int3.2 |char4.2|bool5.2|char6.2
|char1.3|char2.3|int3.3 |char4.3|bool5.3|char6.3
|char1.4|char2.4| | | |char6.4
|char1.5|char2.5| | | |char6.5
|char1.6|char2.6|int3.6 |char4.6|bool5.6|char6.6
So from this table, I would like to extract the columns 3, 4 and 5 (only when there is information, so lines 2,3 and 6) and print the information into another csv file, such as this: 因此,从该表中,我想提取第3、4和5列(仅当有信息时,第2、3和6行),然后将信息打印到另一个csv文件中,例如:
|Column3|Column4|Column5|
|int3.2 |char4.2|bool5.2|
|int3.3 |char4.3|bool5.3|
|int3.6 |char4.6|bool5.6|
strtok is standard function to do that. strtok是执行此操作的标准功能。
#include <stdio.h>
#include <string.h>
int main ()
{
// Read the file ----------
FILE* fp = fopen("toto.csv", "rb");
if (fp == NULL) return 0;
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *pData = new char[size + 1];
fread(pData, sizeof(char), size, fp);
fclose(fp);
// Read the file ----------
// Parse the file content ----------
char* pch;
pch = strtok (pData, "|");
while (pch != NULL)
{
printf ("%s\n", pch);
pch = strtok (NULL, "|");
}
// Parse the file content ----------
return 0;
}
You're welcome ! 别客气 ! If you want to filter on some columns, then, since strtok doesn't take in argument the column number, you will have to use a conditional statement like if, switch (for a specific treatment depending column etc) 如果要在某些列上进行过滤,则由于strtok不接受列号的参数,因此您将必须使用条件语句,例如if,switch(对于取决于列的特定处理等)
For you example: 以您为例:
char* pch;
pch = strtok (pData, "|");
int iCpt = 1;
while (pch != NULL)
{
if (iCpt == 3 || iCpt == 4 || iCpt == 5)
{
printf ("%s\n", pch);
}
pch = strtok (NULL, "|");
iCpt++;
}
Or using switch, allowing a specific treatment on each column: 或使用开关,对每列进行特定处理:
char* pch;
pch = strtok (pData, "|");
int iCpt = 1;
while (pch != NULL)
{
switch(iCpt)
{
case 3:
case 4:
case 5: printf ("%s\n", pch);
break;
default: // Nothing;
}
pch = strtok (NULL, "|");
iCpt++;
}
I hope you will happy with this ;) 我希望你对此感到满意;)
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <sstream>
int main(int argc, char* argv[])
{
std::string csv_File_name = argv[1];
std::ifstream data(csv_File_name);
int row_count =0 ;
std::string line;
while(std::getline(data,line))
{
row_count +=1;
std::stringstream lineStream(line);
std::string cell;
int column_count = 0 ;
while(std::getline(lineStream,cell,','))
{
column_count+=1;
// You have a cell!!!!
if ( column_count == 3 || column_count == 4 || column_count == 5){
std::cout << cell <<" row " << row_count << " column " << column_count<< std::endl;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.