代码拉取完成,页面将自动刷新
/*
* File: CSVFile.h
* Author: Administrator
*
* Created on 2013年7月1日, 下午4:04
*/
#ifndef CSVFILE_H
#define CSVFILE_H
#include <string>
#include <vector>
#include <sstream>
class CSVFile {
public:
bool Load(const std::string& file_name);
bool Save(const std::string& file_name);
bool Save();
void GetRowCol(int& row, int& col) const {
row = row_;
col = col_;
}
template<typename T>
T Read(int row, int col) {
int row_index = row;
if (row_index < 0 || row_index >= static_cast<int> (table_.size())) {
return T();
}
Fields& fields = table_[row_index];
int col_index = col;
if (col_index < 0 || col_index >= static_cast<int> (fields.size())) {
return T();
}
Field& field = fields[col_index];
T data;
std::stringstream ss;
ss << field.value;
ss >> data;
return data;
}
template<typename T>
bool Write(int row, int col, T data, bool is_string = false) {
int row_index = row;
if (row_index < 0) {
table_.push_back(Fields());
row_index = static_cast<int> (table_.size() - 1);
} else {
while (static_cast<int> (table_.size())<(row_index + 1)) {
table_.push_back(Fields());
}
}
Fields& fields = table_[row_index];
int col_index = col;
if (col_index < 0) {
fields.push_back(Field());
col_index = static_cast<int> (fields.size() - 1);
} else {
while (static_cast<int> (fields.size())<(col_index + 1)) {
fields.push_back(Field());
}
}
Field& field = fields[col_index];
std::stringstream ss;
ss << data;
field.value = ss.str();
field.is_string = is_string;
return true;
}
private:
struct Field {
bool is_string;
std::string value;
};
typedef std::vector<Field> Fields;
typedef std::vector<Fields> Table;
void ReadLine(std::fstream& file, Fields& fields);
void WriteLine(std::fstream& file, const Fields& fields);
bool Parse(std::fstream& file);
bool ParseLine(const std::string& line, Fields& fields);
std::string file_name_;
Table table_;
Fields field_;
int row_, col_;
};
#endif /* CSVFILE_H */
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。