Convert Text File to CSV in C
Convert text files to CSV in C with standard fgets and strtok: tokenize each line by your delimiter and write comma-separated output, plus a small helper that quotes fields containing commas or quotes.
Convert text to CSV in C
C has no built-in CSV support, so you tokenize each line yourself with fgets and strtok and write the fields joined by commas. That is straightforward for clean data; if fields can contain commas, you also need to add quoting, shown below.
For a one-off conversion the online tool is far quicker; the C approach suits embedded targets and performance-critical code.
Read with fgets, split with strtok
This reads a tab-delimited file and writes comma CSV, trimming the trailing newline from each line:
#include <stdio.h>
#include <string.h>
int main(void) {
FILE *in = fopen("input.txt", "r");
FILE *out = fopen("output.csv", "w");
if (!in || !out) { perror("open"); return 1; }
char line[4096];
while (fgets(line, sizeof line, in)) {
line[strcspn(line, "\r\n")] = '\0'; /* strip newline */
char *tok = strtok(line, "\t");
int first = 1;
while (tok) {
fprintf(out, "%s%s", first ? "" : ",", tok);
first = 0;
tok = strtok(NULL, "\t");
}
fputc('\n', out);
}
fclose(in);
fclose(out);
return 0;
}Change the strtok delimiter string to " " for spaces or "|" for pipes. Note that strtok treats consecutive delimiters as one, which suits whitespace but can merge empty fields.
Quote fields safely
If a value may contain a comma, quote or newline, write it through a helper that applies CSV quoting (doubling any internal quote):
#include <stdio.h>
#include <string.h>
void write_field(FILE *out, const char *s, int first) {
if (!first) fputc(',', out);
if (strpbrk(s, ",\"\r\n") == NULL) { /* no special chars */
fputs(s, out);
return;
}
fputc('"', out);
for (const char *p = s; *p; p++) {
if (*p == '"') fputc('"', out); /* escape by doubling */
fputc(*p, out);
}
fputc('"', out);
}Call write_field(out, token, first) instead of the plain fprintf above, and the output stays valid even with messy data.
Long lines, memory and encoding
The fixed char line[4096] buffer is simple but truncates very long lines. On POSIX systems, getline grows the buffer automatically:
char *line = NULL;
size_t cap = 0;
ssize_t len;
while ((len = getline(&line, &cap, in)) != -1) {
/* process line ... */
}
free(line);C reads bytes, so UTF-8 passes through unchanged as long as you do not transform individual characters. Just copy the bytes and the encoding is preserved.
Buffers, quoting and verification in C
Two details separate a quick C converter from a robust one. The first is buffer size: a fixed char line[4096] truncates longer lines without warning, so for untrusted input use POSIX getline, which grows the buffer as needed. The second is quoting: strtok plus a comma join produces invalid CSV the moment a field contains a comma or quote, so route every field through a writer that quotes when necessary, as shown above.
Be aware that strtok modifies the buffer in place and collapses consecutive delimiters into one, which is convenient for whitespace but means empty fields between two tabs disappear — if empty columns matter, parse manually with strchr instead. Verify the result by counting commas per line (allowing for quoted commas) or, more simply, by opening the output in a spreadsheet and confirming the columns line up.
Convert Text File to CSV in C — FAQ
How do I convert text to CSV in C?
Read each line with fgets, split it with strtok on your delimiter, and write the fields joined by commas.
How do I handle fields with commas?
Use a helper that wraps such fields in quotes and doubles any internal quote, as shown above.
How do I read very long lines?
Use POSIX getline, which grows the buffer instead of a fixed array.
Does it preserve UTF-8?
Yes. C copies bytes, so UTF-8 text passes through unchanged.