CSV Writer

Header: fiction/utils/io/csv_writer.hpp

class csv_writer

Utility class for writing delimited (e.g. CSV) data into an output stream. It provides a variadic member function, write_line, that can take an arbitrary number of arguments and write them to the provided output stream in a line separated by a specified delimiter.

The csv_writer follows some behavior principles:

  • Any standard data type can be written to the output stream.

  • Data arguments written will be separated by the specified delimiter.

  • A newline is written at the end of each line.

  • If write_line receives no arguments, it only writes a newline.

  • The last value written in a line is not followed by a delimiter.

  • No checks for escape characters are performed.

Example usage:

std::ofstream file("output.csv");
csv_writer writer(file);
writer.write_line("Name", "Age", "City");
writer.write_line("Alice", 20, "New York");

Public Functions

inline explicit csv_writer(std::ostream &os)

Standard constructor.

Parameters:

os – Output stream to write CSV data into.

template<typename T, typename ...Ts>
inline void write_line(T &&arg, Ts&&... args)

Writes one line of delimiter-separated values without a trailing delimiter or flushing the stream. The writer does not escape values. With no arguments, the writer emits a newline.

Template Parameters:
  • T – The type of the first argument.

  • Ts – Types of the rest of the variadic parameter pack.

Parameters:
  • arg – First argument to write to the stream.

  • args – Rest of the arguments to write to the stream if any exist.

template<typename ...Ts>
inline void write_line()

Writes a newline to the output stream. This is the base case of the variadic template function, and is invoked when there are no additional arguments to process.

Template Parameters:

Ts – An empty variadic template argument pack.