猿问

如何读取C中的.csv文件

读取C中的.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来读这篇文章,我有一些代码,但只用于连接。


慕码人8056858
浏览 384回答 3
3回答

动漫人物

一个完整的示例,它将字段保留为原始输入缓冲区中以空结尾的字符串,并通过char指针数组提供对它们的访问。CSV处理器已经确认可以使用“双引号”中的字段,而忽略其中的任何分隔符字符。#include <stdio.h>#include <stdlib.h>#include <string.h>// adjust BUFFER_SIZE to suit longest line&nbsp;#define BUFFER_SIZE 1024 * 1024#define NUM_FIELDS 10#define MAXERRS 5#define RET_OK 0#define RET_FAIL 1#define FALSE 0#define TRUE 1// char* array will point to fieldschar *pFields[NUM_FIELDS];// field offsets into pFields array:#define LP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0#define IMIE&nbsp; &nbsp; &nbsp; &nbsp; 1#define NAZWISKo&nbsp; &nbsp; 2#define ULICA&nbsp; &nbsp; &nbsp; &nbsp;3#define NUMER&nbsp; &nbsp; &nbsp; &nbsp;4#define KOD&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5#define MIEJSCOw&nbsp; &nbsp; 6#define TELEFON&nbsp; &nbsp; &nbsp;7#define EMAIL&nbsp; &nbsp; &nbsp; &nbsp;8#define DATA_UR&nbsp; &nbsp; &nbsp;9long loadFile(FILE *pFile, long *errcount);static int&nbsp; loadValues(char *line, long lineno);static char delim;long loadFile(FILE *pFile, long *errcount){&nbsp; &nbsp; char sInputBuf [BUFFER_SIZE];&nbsp; &nbsp; long lineno = 0L;&nbsp; &nbsp; if(pFile == NULL)&nbsp; &nbsp; &nbsp; &nbsp; return RET_FAIL;&nbsp; &nbsp; while (!feof(pFile)) {&nbsp; &nbsp; &nbsp; &nbsp; // load line into static buffer&nbsp; &nbsp; &nbsp; &nbsp; if(fgets(sInputBuf, BUFFER_SIZE-1, pFile)==NULL)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; // skip first line (headers)&nbsp; &nbsp; &nbsp; &nbsp; if(++lineno==1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; // jump over empty lines&nbsp; &nbsp; &nbsp; &nbsp; if(strlen(sInputBuf)==0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; // set pFields array pointers to null-terminated string fields in sInputBuf&nbsp; &nbsp; &nbsp; &nbsp; if(loadValues(sInputBuf,lineno)==RET_FAIL){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(*errcount)++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(*errcount > MAXERRS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // On return pFields array pointers point to loaded fields ready for load into DB or whatever&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Fields can be accessed via pFields, e.g.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("lp=%s, imie=%s, data_ur=%s\n", pFields[LP], pFields[IMIE], pFields[DATA_UR]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return lineno;}static int&nbsp; loadValues(char *line, long lineno){&nbsp; &nbsp; if(line == NULL)&nbsp; &nbsp; &nbsp; &nbsp; return RET_FAIL;&nbsp; &nbsp; // chop of last char of input if it is a CR or LF (e.g.Windows file loading in Unix env.)&nbsp; &nbsp; // can be removed if sure fgets has removed both CR and LF from end of line&nbsp; &nbsp; if(*(line + strlen(line)-1) == '\r' || *(line + strlen(line)-1) == '\n')&nbsp; &nbsp; &nbsp; &nbsp; *(line + strlen(line)-1) = '\0';&nbsp; &nbsp; if(*(line + strlen(line)-1) == '\r' || *(line + strlen(line)-1 )== '\n')&nbsp; &nbsp; &nbsp; &nbsp; *(line + strlen(line)-1) = '\0';&nbsp; &nbsp; char *cptr = line;&nbsp; &nbsp; int fld = 0;&nbsp; &nbsp; int inquote = FALSE;&nbsp; &nbsp; char ch;&nbsp; &nbsp; pFields[fld]=cptr;&nbsp; &nbsp; while((ch=*cptr) != '\0' && fld < NUM_FIELDS){&nbsp; &nbsp; &nbsp; &nbsp; if(ch == '"') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(! inquote)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pFields[fld]=cptr+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *cptr = '\0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// zero out " and jump over it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inquote = ! inquote;&nbsp; &nbsp; &nbsp; &nbsp; } else if(ch == delim && ! inquote){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *cptr = '\0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// end of field, null terminate it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pFields[++fld]=cptr+1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cptr++;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; if(fld > NUM_FIELDS-1){&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "Expected field count (%d) exceeded on line %ld\n", NUM_FIELDS, lineno);&nbsp; &nbsp; &nbsp; &nbsp; return RET_FAIL;&nbsp; &nbsp; } else if (fld < NUM_FIELDS-1){&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "Expected field count (%d) not reached on line %ld\n", NUM_FIELDS, lineno);&nbsp; &nbsp; &nbsp; &nbsp; return RET_FAIL;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; return RET_OK;}int main(int argc, char **argv){&nbsp; &nbsp;FILE *fp;&nbsp; &nbsp;long errcount = 0L;&nbsp; &nbsp;long lines = 0L;&nbsp; &nbsp;if(argc!=3){&nbsp; &nbsp; &nbsp; &nbsp;printf("Usage: %s csvfilepath delimiter\n", basename(argv[0]));&nbsp; &nbsp; &nbsp; &nbsp;return (RET_FAIL);&nbsp; &nbsp;}&nbsp; &nbsp;&nbsp; &nbsp;if((delim=argv[2][0])=='\0'){&nbsp; &nbsp; &nbsp; &nbsp;fprintf(stderr,"delimiter must be specified\n");&nbsp; &nbsp; &nbsp; &nbsp;return (RET_FAIL);&nbsp; &nbsp;}&nbsp; &nbsp;fp = fopen(argv[1] , "r");&nbsp; &nbsp;if(fp == NULL) {&nbsp; &nbsp; &nbsp; fprintf(stderr,"Error opening file: %d\n",errno);&nbsp; &nbsp; &nbsp; return(RET_FAIL);&nbsp; &nbsp;}&nbsp; &nbsp;lines=loadFile(fp,&errcount);&nbsp; &nbsp;fclose(fp);&nbsp; &nbsp;printf("Processed %ld lines, encountered %ld error(s)\n", lines, errcount);&nbsp; &nbsp;if(errcount>0)&nbsp; &nbsp; &nbsp; &nbsp; return(RET_FAIL);&nbsp; &nbsp; return(RET_OK);&nbsp;}
随时随地看视频慕课网APP
我要回答