From e92d18b51b7e00f457c8b7074c70a6797f4e7523 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 5 Jun 2023 22:07:39 +0300 Subject: [PATCH] csv --- ~/C/read csv/example.csv | 3 +++ ~/C/read csv/read_csv.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 ~/C/read csv/example.csv create mode 100644 ~/C/read csv/read_csv.c diff --git a/~/C/read csv/example.csv b/~/C/read csv/example.csv new file mode 100644 index 0000000..bb39e22 --- /dev/null +++ b/~/C/read csv/example.csv @@ -0,0 +1,3 @@ +id,text,desc +1,ololo,none +2,l33t,hack diff --git a/~/C/read csv/read_csv.c b/~/C/read csv/read_csv.c new file mode 100644 index 0000000..962f5a6 --- /dev/null +++ b/~/C/read csv/read_csv.c @@ -0,0 +1,31 @@ +#include // file handling functions +#include // atoi +#include // 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; +}