C语言进行csv文件数据的读取

C语言进行csv文件数据的读取:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>

int main(){
    FILE *fp = NULL;
    char *line,*record;
    char buffer[20450];//20450这个数组大小也要根据自己文件的列数进行相应修改。

    if((fp = fopen("All-w.csv", "r")) != NULL)
    {
        fseek(fp, 16415L, SEEK_SET);  //定位到第二行,每个英文字符大小为1,16425L这个参数根据自己文件的列数进行相应修改。

        while ((line = fgets(buffer, sizeof(buffer), fp))!=NULL)//当没有读取到文件末尾时循环继续
        {
            record = strtok(line, ",");
            while (record != NULL)//读取每一行的数据
            {
                printf("%s ", record);//将读取到的每一个数据打印出来
                record = strtok(NULL, ",");
            }
        }
        fclose(fp);
        fp = NULL;
    }
}

猜你喜欢

转载自www.cnblogs.com/ming-4/p/11736162.html