如何从C++中读取CSV文件中的一些列? 我想知道如何从一个Excel文件中读取信息,然后将其保存到另一个文件中,使用C++。

如何从C++中读取CSV文件中的一些列? 我想知道如何从一个Excel文件中读取信息,然后将其保存到另一个文件中,使用C++。,c++,csv,C++,Csv,输入文件将数据分成列。每列的第一行是该列的标签。例如: |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| |

输入文件将数据分成列。每列的第一行是该列的标签。例如:

|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
|Column3|Column4|Column5|
|int3.2 |char4.2|bool5.2|
|int3.3 |char4.3|bool5.3|
|int3.6 |char4.6|bool5.6|
从这个表中,我只想在有信息的情况下提取第3、4和5列,第2、3和6行,然后打印信息 另一个csv文件,例如:

|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
|Column3|Column4|Column5|
|int3.2 |char4.2|bool5.2|
|int3.3 |char4.3|bool5.3|
|int3.6 |char4.6|bool5.6|

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;
}
或使用开关,允许对每列进行特定处理:

  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++;
  }

我希望你会对此感到高兴

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;
}
#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;
            }
        }
    }

}
或使用开关,允许对每列进行特定处理:

  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++;
  }

我希望你会对此感到高兴

我觉得我每天都能看到这个问题的不同版本。下面的问题:我觉得我每天都能看到这个问题的不同版本。下面的问题:谢谢你,蒂埃里。你知道我怎么能只打印3栏的信息吗。例如,3,4和5。嗨,阿德里安,你能告诉我这是否可以。谢谢。嗨,蒂埃里,很抱歉没有回答。。。是的,它很完美。非常感谢你;非常感谢你,蒂埃里。你知道我怎么能只打印3栏的信息吗。例如,3,4和5。嗨,阿德里安,你能告诉我这是否可以。谢谢。嗨,蒂埃里,很抱歉没有回答。。。是的,它很完美。非常感谢你;
#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;
            }
        }
    }

}