将.csv文件的一个完整单元格读取到变量中



.csv文件中每行有三个单元格。我正在尝试将每个单元格读入我创建的类的私有成员变量中。我不知道如何将所有单元格A1读入一个字符串变量,然后将B1读入一个双变量,将C3读入另一个单独的双变量。我玩过getline和重载ostream操作符>gt;但无济于事。

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 

总的来说,我有这样的东西:

int main()
{
//open file
fstream file;
file.open("Feb.csv");
// create LinkedTemp object
LinkedTemp list;
// read file into LinkedTemp object
// while (file)
//{
file >> list;
//}
// append the information into the list
// list.append();
// display all the contents of the list
cout << list;
return 0;
}

但我要么读了整行,要么只读了.csv文件的2020-02-07部分,而不是整个2020-02-0700:00:00。有关.csv文件的格式,请参阅附件。csv文件图像

您可以将所有令牌存储在stringsvectorvector中。这将创建一个二维字符串矩阵,您可以稍后随意处理。以下代码是您需要的:

#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::stod(,以防程序被赋予垃圾csv文件。

相关内容

  • 没有找到相关文章

最新更新