ben-strasser / fast-cpp-csv-parser

fast-cpp-csv-parser
BSD 3-Clause "New" or "Revised" License
2.12k stars 439 forks source link

Handling files with optional header lines? #10

Closed intendo closed 8 years ago

intendo commented 8 years ago

What is the best way to write the code to handle the situation where you get a file with the correct format but the header line is missing?

We get the same data from multiple sources and some sources provide a header and some do not.

ben-strasser commented 8 years ago

Hi,

use the

void set_header(some_string_type col_name1, some_string_type col_name2, ...);

function instead of parse_header. The documentation states:

The |set_header| method does /not/ read any input. Use it if the file does not have any header. Obviously it is impossible to rearrange columns or check for their availability when using it. The order in the file and in the program must match when using |set_header|.

Essentially you give the first column the first parameter as name, the second column the second parameter, etc. The column names are only used to format error messages. The number of parameters is used to check that the number of column matches.