基于C語言的開源csv解析庫MiniCSV的使用示例
MiniCSV簡介
之前寫了一篇基于C語言字符串操作函數的csv文件解析:C語言解析csv格式文件,本文介紹一個開源簡潔的csv解析庫的使用:MiniCSV,使用標準C語言設計。
A tiny, fast, simple, single-file, BSD-licensed CSV parsing library in C.
Should be able to handle CSV oddities: multi-lines, escaped rows, escaped characters in escaped rows, empty rows, rows with a variable number of columns, Windows or Unix-style line endings.
Doesn't perform any heap allocations.
一個小型、快速、簡單、單文件、BSD許可的C語言CSV解析庫。
應該能夠處理CSV的奇怪之處:多行、轉義行、轉義列中的轉義字符、空行、列數可變的行、Windows或Unix風格的行結尾。
不執(zhí)行任何堆分配。
開源地址
C語言版本
https://github.com/jedisct1/minicsv
C++版本
https://gitee.com/null_237_8629/minicsv_cpp
只有一個.c和一個.h文件,使用起來非常簡單。
官方示例
#include <stdio.h> #include <stdlib.h> #include "stdint.h" #include "minicsv.h" int display_cols(char **cols, uint8_t cols_count) { printf("cols=%d: ", cols_count); for(int i = 0; i < cols_count; i++) printf("[%s]\t", cols[i]); printf("\n"); return 0; } int main(void) { char *cols[7]; //每行逗號的個數+1 char str_csv[] = "line1, aa, bb, cc, dd, ee, ff\nline2, 11, 22, 33, 44\nline3, 1,2,3,4,5"; char *r = str_csv; uint32_t cols_count; //size_t or uint32_t uint8_t cols_max = sizeof(cols) / sizeof(cols[0]); //最多解析多少列 /* line1, aa, bb, cc, dd, ee, ff line2, 11, 22, 33, 44 line3, 1,2,3,4,5 */ printf("csv string: \n%s\n\n", r); //解析第1行 while(*r != NULL) { r = minicsv_parse_line(r, cols, &cols_count, cols_max); //cols_count=當前行解析出的列數 display_cols(cols, cols_count); } printf("\nfinish!\n"); return 0; }
運行結果:
csv string:
line1, aa, bb, cc, dd, ee, ff
line2, 11, 22, 33, 44
line3, 1,2,3,4,5
cols=7: [line1] [ aa] [ bb] [ cc] [ dd] [ ee] [ ff]
cols=5: [line2] [ 11] [ 22] [ 33] [ 44]
cols=6: [line3] [ 1] [2] [3] [4] [5]
finish!
csv文件解析示例
csv文件內容:
序號,姓名,性別,年齡,職位,兼任,備注
1,張珊,女,29,產品經理
2,李思,男,31,架構師,兼產品副經理,試用
3,王偉,男,27,開發(fā)工程師
4,趙麗,女,27,測試工程師,,實習
minicsv解析示例:
#include "stdio.h" #include "stdlib.h" #include "stdint.h" #include "minicsv.h" #define CSV_PATH "./demo.csv" /* 序號,姓名,性別,年齡,職位,兼任,備注 1,張珊,女,29,產品經理 2,李思,男,31,架構師,兼產品副經理,試用 3,王偉,男,27,開發(fā)工程師 4,趙麗,女,27,測試工程師,,實習 */ int display_cols(char **cols, uint8_t cols_count); int main() { FILE *fp = fopen(CSV_PATH, "rw"); char buf[200]; char *cols[10]; uint8_t cols_max = sizeof(cols) / sizeof(cols[0]); uint32_t cols_count = 0; uint32_t line = 0; while(fgets(buf, sizeof(buf) / sizeof(buf[0]), fp) != NULL) { printf("line = %d, buf = %s", line, buf); minicsv_parse_line(buf, cols, &cols_count, cols_max); line++; display_cols(cols, cols_count); memset(buf, 0, sizeof(buf) / sizeof(buf[0])); } printf("finish! line count = %d\n", line); return 0; } int display_cols(char **cols, uint8_t cols_count) { printf("cols=%d ", cols_count); for(int i = 0; i < cols_count; i++) printf("[%s] ", cols[i]); printf("\n\n"); return 0; }
運行結果:
line = 0, buf = 序號,姓名,性別,年齡,職位,兼任,備注
cols=7 [序號] [姓名] [性別] [年齡] [職位] [兼任] [備注]
line = 1, buf = 1,張珊,女,29,產品經理
cols=5 [1] [張珊] [女] [29] [產品經理]
line = 2, buf = 2,李思,男,31,架構師,兼產品副經理,試用
cols=7 [2] [李思] [男] [31] [架構師] [兼產品副經理] [試用]
line = 3, buf = 3,王偉,男,27,開發(fā)工程師
cols=5 [3] [王偉] [男] [27] [開發(fā)工程師]
line = 4, buf = 4,趙麗,女,27,測試工程師,,實習
cols=7 [4] [趙麗] [女] [27] [測試工程師] [] [實習]
finish! line count = 5
到此這篇關于基于C語言的開源csv解析庫MiniCSV的使用示例的文章就介紹到這了,更多相關C語言 MiniCSV內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++中靜態(tài)初始化數組與動態(tài)初始化數組詳解
今天小編就為大家分享一篇C++中靜態(tài)初始化數組與動態(tài)初始化數組詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07