在 C 中读取 .csv 文件 [英] Read .csv file in C
本文介绍了在 C 中读取 .csv 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 .csv 文件:
I have a .csv file :
lp;imie;nazwisko;ulica;numer;kod;miejscowosc;telefon;email;data_ur
1;Jan;Kowalski;ul. Nowa;1a;11-234;Budry;123-123-456;jan@go.xxx;1980.05.13
2;Jerzy;Nowak;ul. Konopnicka;13a/3;00-900;Lichowice;(55)333-44-55;jer@wu.to;1990.03.23
我需要用 C 语言阅读这个.我有一些代码,但仅用于连接.
and I need to read this in C. I have some code, but only for connection.
推荐答案
希望这能让你开始
在http://ideone.com/l23He(使用标准输入)
See it live on http://ideone.com/l23He (using stdin)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char* getfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, ";
"))
{
if (!--num)
return tok;
}
return NULL;
}
int main()
{
FILE* stream = fopen("input", "r");
char line[1024];
while (fgets(line, 1024, stream))
{
char* tmp = strdup(line);
printf("Field 3 would be %s
", getfield(tmp, 3));
// NOTE strtok clobbers tmp
free(tmp);
}
}
输出:
Field 3 would be nazwisko
Field 3 would be Kowalski
Field 3 would be Nowak
这篇关于在 C 中读取 .csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文