使用multimap在c++中读取并打印一个超过2列的csv文件

Read and print a csv file with more than 2 column in c++ using multimap

本文关键字:一个 2列 文件 csv c++ multimap 读取 打印 使用      更新时间:2023-10-16

我是c++的初学者,需要编写一个c++程序来读取和打印这样的csv文件。

DateTime,value1,value2
12/07/16 13:00,3.60,50000
14/07/16 20:00,4.55,3000

我可以知道如何进行编程吗?我只能通过一个简单的多映射代码来获取日期。

我花了一些时间为您制定了几乎(请阅读最后的通知)精确的解决方案。

我假设您的程序是一个控制台应用程序,它接收原始csv文件名作为命令行参数。

因此,如果您喜欢,请查看以下代码并进行必要的更改:

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

注意:您的任务只是将向量中的一个向量(用于result的类型std::vector<std::vector<std::string>>)替换为多映射(我希望您了解解决方案中的关键)

当然,这项任务有很多可能的解决方案(如果你打开这个问题并仔细查看答案,你就会明白这一点)。

首先,我建议考虑以下例子,并尝试以最简单的方式完成任务:

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

我认为它对你有用。