vincentlaucsb / csv-parser

A high-performance, fully-featured CSV parser and serializer for modern C++.
MIT License
901 stars 150 forks source link

new CSVWriter? #168

Closed ChiFungLam closed 3 years ago

ChiFungLam commented 3 years ago

Sorry, it may be a stupid question, how to NEW a CSVWriter object? Since I need to process a lot of csv files and so I need to create/delete in loop. I try CSVWriter * MyPtr=new CSVWriter; without success. Thanks.

Regds LAM Chi-fung

tonjif commented 3 years ago

It appears that the constructor of CSVWriter accepts a string, or a string and a CSVFormat object, as shown in README.md:

CSVReader reader("very_big_file.csv");

CSVFormat format;
format.delimiter('\t')
      .quote('~')
      .header_row(2);   // Header is on 3rd row (zero-indexed)
      // .no_header();  // Parse CSVs without a header row
      // .quote(false); // Turn off quoting 

// Alternatively, we can use format.delimiter({ '\t', ',', ... })
// to tell the CSV guesser which delimiters to try out

CSVReader reader("wierd_csv_dialect.csv", format);

Thus, one of the two constructors must be called when newing a CSVWriter object.

ChiFungLam commented 3 years ago

I tried CSVWriter MyWriter = new CSVWriter(outfile); where outfile is ofstream but still failed.

ChiFungLam commented 3 years ago

Even I invoke CSVReader and CSVFormat, still not able to new CSVWriter, however, I can new a CSVReader: CSVFormat format; format.no_header(); CSVReader * reader= new CSVReader("MyText.txt", format); and destroy it by delete reader;

vincentlaucsb commented 3 years ago

@tonjif, @ChiFungLam please refer to https://github.com/vincentlaucsb/csv-parser/blob/master/tests/test_write_csv.cpp for examples of instantiating CSVWriters.

The make_csv_writer() helper as shown in the README is also helpful. There is no point in calling new CSVWriter() besides creating more work for yourself. This is a C++ library, not a C library.