Fast and simple CSV parser written in C
Simple and fast library for fast reading of large CSV files using memory-mapped files.
Purpose of this project was to create fast CSV (comma separated values) reader implementation in C with very simple interface using memory-mapped files.
You can add csv.c
file to your project or you can use Makefile provided.
To compile csv library on Linux with GNU Make:
make all
from project root to compile all targets and test applicationError handing ommited for brevity
char* row;
int cols = 0;
CsvHandle handle = CsvOpen("csvfile.csv");
while (row = CsvReadNextRow(handle))
{
/* row = CSV row string */
const char* col;
while (col = CsvReadNextCol(row, handle))
cols++; /* col = CSV col string */
}
printf("Number of cols %i", cols);
If you want to read classic CSV files, you can follow this pipeline:
CsvOpen()
to open CSV fileCsvReadNextRow()
to read single CSV lineCsvReadNextCol()
to read single CSV columnCsvClose()
to close opened CSV handleCsvOpen(const char* filepath)
Opens a CSV file.
const char*
): path to a CSV fileCsvHandle
: handle to a CSV file on success, NULL otherwise
CsvOpen2(const char* filepath, char delim, char quote, char escape)
Opens a CSV file. You can specify custom CSV delimeter, quote and escape char.
const char*
): path to a CSV filechar
): custom CSV delimeter ASCII character (default ‘,’)char
): custom CSV quote ASCII character (default ‘"’)char
): custom CSV escape ASCII character (default ‘\’)CsvHandle
: handle to a CSV file on success, NULL otherwise
CsvClose(CsvHandle handle)
Releases all resources allocated.
CsvHandle
): handle opened by CsvOpen() or CsvOpen2()CsvReadNextRow(CsvHandle handle)
Returns pointer to new line (UTF-8 zero terminated string) or NULL.
CsvHandle
): handle opened by CsvOpen() or CsvOpen2()char*
: zero terminated string on success, NULL on EOF or error.
CsvReadNextCol(CsvHandle handle, char* row)
Returns pointer to column (UTF-8 zero terminated string) or NULL
CsvHandle
): handle opened by CsvOpen() or CsvOpen2()const char*
: zero terminated string on success, NULL on EOL or error.
MIT (see LICENSE.txt)