This commit is contained in:
Alexander Popov 2023-06-05 22:07:39 +03:00
parent 29bf8dd43b
commit e92d18b51b
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
2 changed files with 34 additions and 0 deletions

3
~/C/read csv/example.csv Normal file
View File

@ -0,0 +1,3 @@
id,text,desc
1,ololo,none
2,l33t,hack
1 id text desc
2 1 ololo none
3 2 l33t hack

31
~/C/read csv/read_csv.c Normal file
View File

@ -0,0 +1,31 @@
#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;
}