jehugaleahsa / FlatFiles

Reads and writes CSV, fixed-length and other flat file formats with a focus on schema definition, configuration and speed.
The Unlicense
357 stars 64 forks source link

Support for empty separated values file write with headers #28

Closed POFerro closed 6 years ago

POFerro commented 6 years ago

SeparatedValueWriter with IsFirstRecordSchema = true option doesn't write the schema as the first line if the file is empty. Is it possible to have a way to write the first line (file header) even if there are no records to write?

Best regards

jehugaleahsa commented 6 years ago

Ha, I had the exact opposite request from someone else as part of the last major upgrade. I used to always write the header even if nothing was done.

The issue is that you create a writer using a simple constructor, and constructors cannot be async. People writing async code do not expect to go out to the hard drive when creating an object, so the solution was to only write the header when writing the first line.

I totally see where you're coming from, so let me do some research. It's almost like I need an explicit method on the writer to write the header if it hasn't been already. That way you can do it explicitly if you need it no matter what.

POFerro commented 6 years ago

Hi Travis,

Thanks for the quick reply, yeah, I see what you mean by not writing the header during object construction. A method to explicitly write the header or something like a WriteEmpty method would do the trick for me ;).

Best regards

jehugaleahsa commented 6 years ago

I just put out a new version. Both writer classes have a new method named WriteSchema and WriteSchemaAsync that will write the schema (if there is one) to the file. Note that it will only write the schema if no other records have been written yet, so make sure you call it prior to any calls to Write.

POFerro commented 6 years ago

Hi Travis,

Thanks for the quick solution, it works perfectly for me.

Best regards