From 4b752b57d2b34a1944386c0234a2ca1b67ea4532 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 5 Jun 2023 22:13:03 +0300 Subject: [PATCH] read csv --- content/posts/2023/c/read-csv.md | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 content/posts/2023/c/read-csv.md diff --git a/content/posts/2023/c/read-csv.md b/content/posts/2023/c/read-csv.md new file mode 100644 index 0000000..f9c520d --- /dev/null +++ b/content/posts/2023/c/read-csv.md @@ -0,0 +1,44 @@ +--- +title: "♾️ Чтение CSV на Си" +date: 2023-06-05T22:08:07+03:00 +draft: false +tags: [c, tips] +--- + +## Файл CVS + +```text +id,text,from +1,text message,twitter +2,i live this language,book +``` + +## Код + +```c +#include +#include + +int main() { + char buffer[80]; + FILE *stream = fopen("example.csv", "r"); + + while (fgets(buffer, 80, stream)) { + // Символ разделитель + char *token = strtok(buffer, ","); + + // Если нам нужен только первый столбец каждой строки + if (token) { + printf("%s\n", token); + } + + // Если нам нужны все значения подряд + while (token) { + printf("%s\n", token); + token = strtok(NULL, ","); + } + } + + return 0; +} +```