C:
Working with CSV

How to:

Reading CSV Files

To read a CSV file in C, we use standard file I/O functions along with string manipulation functions to parse each line. Below is a basic example of reading a CSV file and printing each row’s fields to the console.

#include <stdio.h>
#include <string.h>

int main() {
    FILE *fp = fopen("data.csv", "r");
    if (!fp) {
        printf("Can't open file\n");
        return 1;
    }

    char buf[1024];
    while (fgets(buf, 1024, fp)) {
        char *field = strtok(buf, ",");
        while(field) {
            printf("%s\n", field);
            field = strtok(NULL, ",");
        }
    }

    fclose(fp);
    return 0;
}

Sample data.csv:

Name,Age,Occupation
John Doe,29,Software Engineer

Sample Output:

Name
Age
Occupation
John Doe
29
Software Engineer

Writing to CSV Files

Similarly, writing to a CSV file involves using fprintf to save data in a comma-separated format.

#include <stdio.h>

int main() {
    FILE *fp = fopen("output.csv", "w");
    if (!fp) {
        printf("Can't open file\n");
        return 1;
    }

    char *headers[] = {"Name", "Age", "Occupation", NULL};
    for (int i = 0; headers[i] != NULL; i++) {
        fprintf(fp, "%s%s", headers[i], (headers[i+1] != NULL) ? "," : "\n");
    }
    fprintf(fp, "%s,%d,%s\n", "Jane Doe", 27, "Data Scientist");

    fclose(fp);
    return 0;
}

Sample output.csv Content:

Name,Age,Occupation
Jane Doe,27,Data Scientist

Deep Dive

The CSV format, though seemingly straightforward, comes with its nuances, such as handling commas within fields and encapsulating fields with quotes. The rudimentary examples shown do not account for such complexities, nor do they handle potential errors robustly.

Historically, CSV handling in C has largely been manual due to the language’s low-level nature and lack of built-in high-level abstractions for such tasks. This manual management includes opening files, reading lines, splitting strings, and converting data types as needed.

While direct manipulation of CSV files in C provides valuable learning experiences on file I/O and string handling, several modern alternatives promise efficiency and less error-prone processes. Libraries like libcsv and csv-parser offer comprehensive functions for reading and writing CSV files, including support for quoted fields and custom delimiters.

Alternatively, when working within ecosystems that support it, integrating with languages or platforms that provide high-level CSV manipulation functions (like Python with its pandas library) can be a more productive route for applications requiring heavy CSV processing. This cross-language approach leverages C’s performance and systems programming capabilities while utilizing the ease of use from other languages for specific tasks such as CSV handling.