snipplets.dev/~/C/read csv/read_csv.c

32 lines
887 B
C

#include <stdio.h> // file handling functions
#include <stdlib.h> // atoi
#include <string.h> // strtok
int main() {
char buffer[80];
FILE *stream = fopen("example.csv", "r");
while (fgets(buffer, 80, stream)) {
char *token = strtok(buffer, ",");
// If you only need the first column of each row
// Если нам нужен только первый столбец каждой строки
if (token) {
int n = atoi(token);
printf("%s\n", n);
}
// If you need all the values in a row
// Если нам нужны все значения подряд
while (token) {
// Just printing each integer here but handle as needed
// int n = atoi(token);
printf("%s\n", token);
token = strtok(NULL, ",");
}
}
return 0;
}