C++ 将.csv文件的整个单元格读入变量

C++ 将.csv文件的整个单元格读入变量,c++,list,csv,file,C++,List,Csv,File,在.csv文件中,每行有三个单元格。我试图将每个单元格读入我创建的类的私有成员变量中。我不知道如何将所有单元格A1读入一个字符串变量,然后B1读入一个双变量,C3读入另一个单独的双变量。我已经尝试过使用getline和重载ostream操作符>>,但没有任何效果 class LinkedTemp { private: // the struct that will temporarily hold all items being read from file struct Govee { st

在.csv文件中,每行有三个单元格。我试图将每个单元格读入我创建的类的私有成员变量中。我不知道如何将所有单元格A1读入一个字符串变量,然后B1读入一个双变量,C3读入另一个单独的双变量。我已经尝试过使用getline和重载ostream操作符>>,但没有任何效果

class LinkedTemp
{
private: 
// the struct that will temporarily hold all items being read from file
struct Govee
{
string date;
double temperature;
double humidity;
struct Govee *next;
};

Govee *head; // pointer to head of all ListNodes

string time;
double temp;
double humi;

public:
// default constructor 
// set private members 
LinkedTemp()
{
head = nullptr;

time = "";
temp = 0.0;
humi = 0.0;

}

void append()
{
Govee *nodePtr, *newNode = nullptr;

// create a newNode and fill it with data (node will then be appended)
newNode = new Govee;
newNode->date = this->time;
newNode->temperature = this->temp;
newNode->humidity = this->humi;
newNode->next = nullptr;    

// start at beginning of list
nodePtr = head;

// if list does not exist, make node the first node
if (!head)
head = newNode;


else
{
// traverse the list
while (nodePtr != nullptr)
    nodePtr = nodePtr->next;

// append newNode onto the end of the list
nodePtr->next = newNode;
}//end else statement

}//end append function


// supports 
// cout << object
friend ostream &operator << (ostream &strm, LinkedTemp &r)
{
strm << "Date: " << r.time << endl;
strm << "Temp: " << r.temp << endl;
strm << "Humi: " << r.humi << endl;
return strm;
}

// will fill all public member variables of class 
// file >> object
friend istream &operator >> (istream &strm, LinkedTemp &r)
{   
getline(strm, r.time);
strm >> r.temp;
strm >> r.humi;
return strm;
}

}; // end class 
class-LinkedTemp
{
私人:
//将临时保存从文件中读取的所有项的结构
结构政府
{
字符串日期;
双温;
双湿度;
结构Govee*下一步;
};
Govee*head;//指向所有ListNodes的头的指针
串时间;
双温;
双呼米;
公众:
//默认构造函数
//设置私人成员
LinkedTemp()
{
水头=零PTR;
时间=”;
温度=0.0;
humi=0.0;
}
void append()
{
Govee*nodePtr,*newNode=nullptr;
//创建一个新节点并用数据填充它(然后将附加节点)
newNode=newgovee;
新建节点->日期=此->时间;
新建节点->温度=此->温度;
新建节点->湿度=此->湿度;
newNode->next=nullptr;
//从列表的开头开始
nodePtr=头部;
//如果列表不存在,则将节点设为第一个节点
如果(!头)
头=新节点;
其他的
{
//遍历列表
while(nodePtr!=nullptr)
nodePtr=nodePtr->next;
//将newNode追加到列表的末尾
nodePtr->next=newNode;
}//end else语句
}//结束附加函数
//支持

//cout您可以通过将所有令牌存储在
向量的
向量的
字符串的
中来实现。这将创建一个二维字符串矩阵,您可以在以后以任何方式处理该矩阵。以下代码是您需要的:

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

struct data{
    std::string s0;
    double d0;
    double d1;
};

int main()
{
    // create a vector of your class to store all of your data, 
    // in your case it's a list so you can tweak it according to your needs
    std::vector <data> vec_data;
    // create a vector of vectors of strings to store all the tokens you read from file
    std::vector <std::vector<std::string>> vec_tokens;

    std::ifstream is("file.csv");
    if(is.is_open()) {
        std::string line;
        while(getline(is, line)) {
            std::stringstream ss_line(line);
            std::string token;
            std::vector <std::string> vec_temp;
            while(getline(ss_line, token, ',')) {
                vec_temp.push_back(token);
            }
            vec_tokens.push_back(vec_temp);
        }
    }

    // convert all tokens into data
    for(auto i: vec_tokens) {
        vec_data.push_back({i[0], std::stod(i[1]), std::stod(i[2])});   
    }

    // clean up tokens
    vec_tokens.clear();

    // check if it worked
    for(auto i: vec_data) {
        std::cout << i.s0 << " , " << i.d0 << " , " <<  i.d1    << std::endl;
    }


    return 0;
}
#包括
#包括
#包括
#包括
结构数据{
std::字符串s0;
双d0;
双d1;
};
int main()
{
//创建类的向量以存储所有数据,
//在您的情况下,它是一个列表,因此您可以根据需要调整它
std::向量向量向量数据;
//创建字符串向量向量以存储从文件读取的所有标记
std::向量向量向量标记;
std::ifstream是(“file.csv”);
如果(is.is_open()){
std::字符串行;
while(getline(is,line)){
标准::stringstream不锈钢线(线);
字符串标记;
标准:向量向量温度;
while(getline(ss_行,token,',')){
车辆温度回推(令牌);
}
vec_代币。推回(vec_临时);
}
}
//将所有令牌转换为数据
用于(自动i:vec_令牌){
vec_data.push_back({i[0],std::stod(i[1]),std::stod(i[2]);
}
//清理代币
vec_令牌。清除();
//检查它是否有效
用于(自动i:vec_数据){

std::cout首先,显示的代码调用getline。这会将整行读入
时间
。所有三个值。然后显示的代码在输入流上再次使用
>
运算符。这对您有意义吗?因此,csv文件的第一行在字符串中。这将成为“时间”。相反,代码使用
>
读取第一行后面的任何内容,作为csv文件中以空格分隔的单词。总计:第一行读取,成为字符串。
>
读取第一行后面必须以空格分隔的两个单词。文件的其余部分被忽略。正如斯波克先生所说:这是非常不合逻辑的。Co你能缩进你的代码吗?第一栏的内容很难阅读。@John Kugelman-谢谢!
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

struct data{
    std::string s0;
    double d0;
    double d1;
};

int main()
{
    // create a vector of your class to store all of your data, 
    // in your case it's a list so you can tweak it according to your needs
    std::vector <data> vec_data;
    // create a vector of vectors of strings to store all the tokens you read from file
    std::vector <std::vector<std::string>> vec_tokens;

    std::ifstream is("file.csv");
    if(is.is_open()) {
        std::string line;
        while(getline(is, line)) {
            std::stringstream ss_line(line);
            std::string token;
            std::vector <std::string> vec_temp;
            while(getline(ss_line, token, ',')) {
                vec_temp.push_back(token);
            }
            vec_tokens.push_back(vec_temp);
        }
    }

    // convert all tokens into data
    for(auto i: vec_tokens) {
        vec_data.push_back({i[0], std::stod(i[1]), std::stod(i[2])});   
    }

    // clean up tokens
    vec_tokens.clear();

    // check if it worked
    for(auto i: vec_data) {
        std::cout << i.s0 << " , " << i.d0 << " , " <<  i.d1    << std::endl;
    }


    return 0;
}