This commit is contained in:
Alexander Popov 2023-06-05 22:13:03 +03:00
parent 4da03f099c
commit 4b752b57d2
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
1 changed files with 44 additions and 0 deletions

View File

@ -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 <stdio.h>
#include <string.h>
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;
}
```