使用multimap在c++中读取并打印一个超过2列的csv文件
Read and print a csv file with more than 2 column in c++ using multimap
我是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;
}
我认为它对你有用。
相关文章:
- 一个 2D 数组,并按行存储值.第一个循环用于行索引,第二个循环用于列索引
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- 如何创建一个具有可变行且每行有 3 列的 2D 数组?
- 如何将 50 行和 50 列的二维数组编码为一个为元素随机分配星号的函数?
- 从两列中选择一个数字并进行计算
- 我可以在每行中有一个不同数量的列数组吗?
- 显示数组的问题.一切都放在一个单独的行中,但不放入单独的列中
- 是否可以通过一个模板函数实现列操作和行操作?
- 选项卡到qtableView中的下一个可见列
- 从给定的行和列中生成一个2D整数数组
- 对于自定义类的一个未定义集,是否有一个默认的散列函数
- 从矩阵复制列向量到OpenCV中另一个矩阵的每个col
- OpenCV 中的函数可同时查找任何一个维度(行/列)的平均值/平均值
- 在std::unordered_set:上,请求一个具有默认散列的类型列表
- 如何按第一个,然后是第二个,然后是第三个对 2d 数组进行排序,..使用 C++ 的列
- 一个很好的向量散列函数
- 每隔一行或每一列提取一个特征矩阵作为一个新矩阵
- 我在一个简单控制台程序(c++)中的第一列代码最终成为我的最后一列
- QTableView,多列带一个标题
- 需要一个散列函数来创建ipv6 16字节地址和TCP 2字节端口号的32位值