Skip to content
This repository has been archived by the owner on Apr 19, 2020. It is now read-only.

Issue with the CSV header #8

Closed
amirmasoudabdol opened this issue Jun 3, 2019 · 4 comments
Closed

Issue with the CSV header #8

amirmasoudabdol opened this issue Jun 3, 2019 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@amirmasoudabdol
Copy link
Contributor

I'm trying to use the csv::Writer as a backend for my Persistent Manager class but for some reason I cannot get it to write the header. Basically, I have something like this:

class PM {
	class Writer;
}

class Writer{

	using namespace std;

	string filename;
	unique_ptr<csv::Writer> writer;

	Writer(string filename) filename(filename) {
		writer = make_unique<csv::Writer>(filename);
		writer->configure_dialect().delimiter(cols);
	}

	~Write(){
		writer->close();
	}

	void write(map<sting, string> row) {
               // I override the method to accept vector<string> as well.
		writer->configure_dialect().column_names(cols);

		writer->write_row(row);
	}
}

Do you see any particular issue with this? I'm not quite sure why honestly, everything looks fine.

@amirmasoudabdol amirmasoudabdol changed the title Issue with the header Issue with the CSV header Jun 3, 2019
@p-ranav p-ranav self-assigned this Jun 3, 2019
@p-ranav p-ranav added the bug Something isn't working label Jun 3, 2019
@p-ranav
Copy link
Owner

p-ranav commented Jun 3, 2019

I've pushed a fix for this. Can check if it's working now?

@amirmasoudabdol
Copy link
Contributor Author

That indeed fixes the issue. Thanks. I wonder if you would like the idea of this override for the column_names. I use a simple method like below. You can probably adjust it to fit the code better.

    Dialect& column_names(const std::vector<std::string> &column_names) {
      column_names_ = column_names;
      return *this;
    }

p-ranav added a commit that referenced this issue Jun 4, 2019
@p-ranav
Copy link
Owner

p-ranav commented Jun 4, 2019

I've pushed an update to support this.

#include <csv/writer.hpp>

int main() {
  csv::Writer foo("test.csv");
  foo.configure_dialect()
    .delimiter(", ")
    .column_names(std::vector<std::string>{"a", "b", "c"});
  foo.write_row("1", "2", "3");
  foo.close();
  return 0;
}

Hope this meets your needs.

@amirmasoudabdol
Copy link
Contributor Author

That would work! 👍🏼

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants