[英]Read and print a csv file with more than 2 column in c++ using multimap
I'm a beginner in c++ and required to write a c++ program to read and print a csv file like this. 我是c ++的初学者,需要编写一个c ++程序来读取和打印这样的csv文件。
DateTime,value1,value2
12/07/16 13:00,3.60,50000
14/07/16 20:00,4.55,3000
May I know how can I proceed with the programming? 我可以知道如何进行编程吗? I manage to get the date only via a simple multimap code.
我只能通过一个简单的多图代码来获取日期。
I spent some time to make almost (read notice at the end) exact solution for you. 我花了一些时间为您提供几乎(确切的阅读说明)准确的解决方案。
I assume that your program is a console application that receives the original csv-file name as a command line argument. 我假设您的程序是一个控制台应用程序,该应用程序接收原始的csv文件名作为命令行参数。
So see the following code and make required changes if you like: 因此,请根据需要查看以下代码并进行必要的更改:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <string>
std::vector<std::string> getLineFromCSV(std::istream& str, std::map<int, int>& widthMap)
{
std::vector<std::string> result;
std::string line;
std::getline(str, line);
std::stringstream lineStream(line);
std::string cell;
int cellCnt = 0;
while (std::getline(lineStream, cell, ','))
{
result.push_back(cell);
int width = cell.length();
if (width > widthMap[cellCnt])
widthMap[cellCnt] = width;
cellCnt++;
}
return result;
}
int main(int argc, char * argv[])
{
std::vector<std::vector<std::string>> result; // table with data
std::map<int, int> columnWidths; // map to store maximum length (value) of a string in the column (key)
std::ifstream inpfile;
// check file name in the argv[1]
if (argc > 1)
{
inpfile.open(argv[1]);
if (!inpfile.is_open())
{
std::cout << "File " << argv[1] << " cannot be read!" << std::endl;
return 1;
}
}
else
{
std::cout << "Run progran as: " << argv[0] << " input_file.csv" << std::endl;
return 2;
}
// read from file stream line by line
while (inpfile.good())
{
result.push_back(getLineFromCSV(inpfile, columnWidths));
}
// close the file
inpfile.close();
// output the results
std::cout << "Content of the file:" << std::endl;
for (std::vector<std::vector<std::string>>::iterator i = result.begin(); i != result.end(); i++)
{
int rawLen = i->size();
for (int j = 0; j < rawLen; j++)
{
std::cout.width(columnWidths[j]);
std::cout << (*i)[j] << " | ";
}
std::cout << std::endl;
}
return 0;
}
NOTE: Your task is just to replace a vector of vectors (type std::vector<std::vector<std::string>>
that are used for result
) to a multimap (I hope you understand what should be a key in your solution) 注意:您的任务只是将向量的向量(用于
result
类型std::vector<std::vector<std::string>>
)替换为多图(希望您了解在您的解决方案)
Of course, there are lots of possible solutions for that task (if you open this question and look through the answers you will understand this). 当然,该任务有很多可能的解决方案(如果您打开此问题并仔细查看答案,您将理解这一点)。
First of all, I propose to consider the following example and to try make your task in the simplest way: 首先,我建议考虑以下示例,并尝试以最简单的方式完成任务:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string str = "12/07/16 13:00,3.60,50000";
stringstream ss(str);
vector<string> singleRow;
char ch;
string s = "";
while (ss >> ch)
{
s += ch;
if (ss.peek() == ',' || ss.peek() == EOF )
{
ss.ignore();
singleRow.push_back(s);
s.clear();
}
}
for (vector<string>::iterator i = singleRow.begin(); i != singleRow.end(); i++)
cout << *i << endl;
return 0;
}
I think it can be useful for you. 我认为这可能对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.