【问题标题】:how to read data from different cells of Excel (csv) file in C++..?如何从 C++ 中的 Excel (csv) 文件的不同单元格中读取数据..?
【发布时间】:2014-01-18 16:09:18
【问题描述】:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    string a1[100];
    string a2[100];
    string a3[100];
    ifstream in;
    ofstream out;
    out.open("E:\\abc.csv");
    out << "dcsdc,dcdcdc,dcsdc" << endl;
    out << "dcsdc,sdcsdc,sdcdc" << endl;
    in.open("E:\\abc.csv");
    /*
        Read from different cells
    */


}

我不知道要写什么来从三个不同的单元格中读取数据并将它们存储在 a1、a2、a3 中。

对于 .csv 文件 ' ,'(逗号)是分隔符。

【问题讨论】:

  • 要读取数据到一个分隔符,假设这个分隔符不能是内容的一部分,考虑std::getline
  • 在 StackOverflow 中搜索“[c++] 读取 csv 文件”。这里至少有10个答案:google.com/…
  • c++ Read from .csv file的可能重复

标签: c++


【解决方案1】:

尝试以下(借用自here),它将根据,将每一行拆分为您想要的不同单元格。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream  data("plop.csv");

    std::string line;
    while(std::getline(data,line))
    {
        std::stringstream  lineStream(line);
        std::string        cell;
        while(std::getline(lineStream,cell,','))
        {
            // You have a cell!!!!
        }
    }
 }

另见这个问题:CSV parser in C++

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多