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.
- Simple C interface
- Very large CSV file support - GBs, TBs
- Using memory mapped files
- Supports UNIX and Windows platforms
- UTF-8 support
- Supports both Windows CRLF "\r\n" and Unix LF "\n" sequences
- Supports newlines "\n" in CSV columns
- Spaces are preserved (e.g "one, two" -> {"one", " two"})
You can add csv.c
file to your project or you can use Makefile provided.
To compile csv library on Linux with GNU Make:
- run
make all
from project root to compile all targets and test application
Error 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 handle
Opens a CSV file.
- filepath, (
const char*
): path to a CSV file
CsvHandle
: handle to a CSV file on success, NULL otherwise
Opens a CSV file. You can specify custom CSV delimeter, quote and escape char.
- filepath, (
const char*
): path to a CSV file - delim (
char
): custom CSV delimeter ASCII character (default ',') - quote (
char
): custom CSV quote ASCII character (default '"') - escape (
char
): custom CSV escape ASCII character (default '\')
CsvHandle
: handle to a CSV file on success, NULL otherwise
Releases all resources allocated.
- handle (
CsvHandle
): handle opened by CsvOpen() or CsvOpen2()
Returns pointer to new line (UTF-8 zero terminated string) or NULL.
- handle (
CsvHandle
): handle opened by CsvOpen() or CsvOpen2()
char*
: zero terminated string on success, NULL on EOF or error.
Returns pointer to column (UTF-8 zero terminated string) or NULL
- handle (
CsvHandle
): handle opened by CsvOpen() or CsvOpen2()
const char*
: zero terminated string on success, NULL on EOL or error.
MIT (see LICENSE.txt)