-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVtoJSON.c
More file actions
29 lines (24 loc) · 827 Bytes
/
CSVtoJSON.c
File metadata and controls
29 lines (24 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <string.h>
int main(void) {
char line[256];
printf("[\n"); // JSON-taulukon alku
int first = 1;
// Luetaan rivejä stdinistä kunnes EOF
while (fgets(line, sizeof(line), stdin)) {
char firstname[128] = {0};
char lastname[128] = {0};
char birthyear[16] = {0};
// Parsitaan CSV: "Etunimi;Sukunimi;Vuosi"
if (sscanf(line, "%127[^;];%127[^;];%15s", firstname, lastname, birthyear) == 3) {
if (!first) {
printf(",\n"); // erotin edellisten rivien jälkeen
}
printf(" {\"firstname\":\"%s\", \"lastname\":\"%s\", \"birthyear\":\"%s\"}",
firstname, lastname, birthyear);
first = 0;
}
}
printf("\n]\n"); // JSON-taulukon loppu
return 0;
}