csv
This commit is contained in:
parent
29bf8dd43b
commit
e92d18b51b
3
~/C/read csv/example.csv
Normal file
3
~/C/read csv/example.csv
Normal file
@ -0,0 +1,3 @@
|
||||
id,text,desc
|
||||
1,ololo,none
|
||||
2,l33t,hack
|
|
31
~/C/read csv/read_csv.c
Normal file
31
~/C/read csv/read_csv.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user