29 #include <boost/format.hpp>
30 #include <boost/tokenizer.hpp>
48 std::istream &
operator>>(std::istream &ins, std::vector<T> &record) {
54 std::getline(ins, line);
57 std::string separator_characters(
", \t");
58 boost::escaped_list_separator<char> separators(empty, separator_characters, empty);
59 boost::tokenizer<boost::escaped_list_separator<char>> tk(line, separators);
61 for (boost::tokenizer<boost::escaped_list_separator<char>>::iterator i(tk.begin()); i != tk.end(); ++i) {
62 T f = std::strtod(i->c_str(), 0);
75 inline std::istream &
operator>>(std::istream &ins, std::vector<std::vector<T> > &data) {
80 std::vector<T> record;
81 while (ins >> record) {
82 data.push_back(record);
105 std::ifstream infile;
106 infile.open(filename);
108 if (infile.is_open()) {
112 catch (std::exception &e) {
113 throw std::runtime_error(
114 (boost::format(
"Error when parsing csv file %s.\nDetails from tokenizer:\n\t%s") %
115 filename % e.what()).str()
122 throw std::runtime_error(
123 (boost::format(
"Error opening file %s") % filename).str()
137 void write(
const std::string &filename,
138 const std::vector<std::vector<T> > &data,
139 unsigned int precision,
140 const std::string &delimiter) {
141 std::ofstream output_file;
142 output_file.open(filename);
145 if (!output_file.is_open()) {
146 throw std::runtime_error(
147 (boost::format(
"Error opening file %s") % filename).str()
151 for (
size_t i = 0; i < data.size(); ++i) {
152 num_cols = data[i].size();
153 for (
size_t j = 0; j < num_cols; ++j) {
154 output_file << std::fixed << std::setprecision(precision) << data[i][j];
155 if (j < num_cols - 1) {
156 output_file << delimiter;
167 #endif //CSV_PARSER_H
void write(const std::string &filename, const std::vector< std::vector< T > > &data, unsigned int precision, const std::string &delimiter)
Definition: csv_parser.h:137
void parseToVector(std::string filename, std::vector< T > &data)
parses the contents of file_name into data.
Definition: csv_parser.h:103
std::istream & operator>>(std::istream &ins, std::vector< T > &record)
Definition: csv_parser.h:48
Reads data from a csv file into an std::vector and writes the contents of an std::vector to a file...
Definition: csv_parser.h:93
Definition: containers.h:41