C语言读取csv文件并保存到二维数组

fgets获取csv数据

#include <stdio.h>
#include <string.h>
#include <time.h>
#define MAXCHAR 1024
#define MAXCOUNT 1000000
char *mat[MAXCOUNT][9]; // 如果放到main里面会有长度限制使应用程序退出,放在外面作为全局变量没有限制。

int main()
{
    clock_t start, end;
    start = clock();

    FILE *fp;
    char row[MAXCHAR];
    char *token;

    fp = fopen("big.csv", "r");
    int linecount = 0, i = 0, j = 0;

    while (fgets(row, MAXCHAR, fp))
    {
        linecount++;
        if (linecount == 1)
            continue; // Skip first line

        if (linecount - 1 > MAXCOUNT) // 实际输出的行数 要去掉上面跳过的1行
            break;

        token = strtok(row, ",");
        j = 0;
        while (token)
        {
            // printf("Token: %s\n", token);
            // printf("i=%d,j=%d\n", i, j);
            mat[i][j++] = strdup(token);
            token = strtok(NULL, ",");
        }
        i++;
    }
    fclose(fp);

    // for (int i = 0; i < MAXCOUNT; i++)
    // {
    //     for (int j = 0; j < 9; j++)
    //     {
    //         printf("mat[%d][%d]=%s\n", i, j, mat[i][j]);
    //     }
    // }

    puts(mat[405000][3]);

    end = clock();
    printf("time=%f\n", (double)(end - start) / CLOCKS_PER_SEC); // 读取速度跟python的pandas差不多
    return 0;
}

如果要跳过N行,参考下面代码

#include <stdio.h>
#define MAX_LINE_SIZE 1024
int main(void)
{
    char strLine[MAX_LINE_SIZE];

    FILE *fp;
    fp = fopen("hello.txt", "r");
    int skiprows = 3;
    while (fgets(strLine, MAX_LINE_SIZE, fp) && skiprows--); // 跳过N行
    do
    {
        puts(strLine);
    } while (fgets(strLine, MAX_LINE_SIZE, fp));
    fclose(fp);
    return 0;
}

fscanf 获取csv数据

#include <string.h>
#include <stdio.h>
#define MAXLEN 20
struct csv
{
   char date[19];
   char symbol[10];
   float open;
   float high;
   float low;
   float close;
   float open_interest;
   float volume;
   float settlement_price;
};
int main()
{
   FILE *fp;
   fp = fopen("hello.txt", "r");

   char buffer[1024];
   fgets(buffer, 1024, fp); // fgets 将获取一行,并将文件指针设置为从下一行开始。然后,您可以在第一行之后开始阅读您想要的内容。

   struct csv rows[MAXLEN];
   size_t count = 0;
   for (; count < MAXLEN; ++count)
   {
      int got = fscanf(fp, "%[^,],%[^,],%f,%f,%f,%f,%f,%f,%f", rows[count].date, rows[count].symbol, &rows[count].open, &rows[count].high, &rows[count].low, &rows[count].close, &rows[count].open_interest, &rows[count].volume, &rows[count].settlement_price);
      // printf("%d\n", got);
      if (got != 9)
         break; // wrong number of tokens - maybe end of file

      printf("%s,%s,%f,%f,%f,%f,%f,%f,%f\n", rows[count].date, rows[count].symbol, rows[count].open, rows[count].high, rows[count].low, rows[count].close, rows[count].open_interest, rows[count].volume, rows[count].settlement_price);
   }

   fclose(fp);
   return 0;
}
速度: 40多万条数据,读取速度比fgets慢300毫秒左右, 但这里已经直接把数据格式好了

fets 保存一维数组的自定义结构

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

#include <time.h>
#define MAXLEN 1000000
struct csv
{
   char *date;
   char *symbol;
   float open;
   float high;
   float low;
   float close;
   float open_interest;
   float volume;
   float settlement_price;
};
struct csv rows[MAXLEN];

int main()
{
   clock_t start, end;
   start = clock();

   FILE *fp;
   fp = fopen("big.csv", "r");
   char *token;
   char buffer[1024];
   size_t count = 0, skiprows = 1;
   while (fgets(buffer, 1024, fp) && --skiprows)
      ;
   while (fgets(buffer, 1024, fp) && count < MAXLEN)
   {
      // printf("%d: %s\n", count, buffer);
      token = strtok(buffer, ",");
      rows[count].date = strdup(token);
      token = strtok(NULL, ",");
      rows[count].symbol = strdup(token);
      token = strtok(NULL, ",");
      rows[count].open = atof(token);
      token = strtok(NULL, ",");
      rows[count].high = atof(token);
      token = strtok(NULL, ",");
      rows[count].low = atof(token);
      token = strtok(NULL, ",");
      rows[count].close = atof(token);
      token = strtok(NULL, ",");
      rows[count].open_interest = atof(token);
      token = strtok(NULL, ",");
      rows[count].volume = atof(token);
      token = strtok(NULL, ",");
      rows[count].settlement_price = atof(token);
      ++count;
   }
   fclose(fp);

   // for (int i=0;i<MAXLEN;++i){
   //    printf("%s,%s,%f\n",rows[i].date,rows[i].symbol,rows[i].open);
   // }
   int last = count - 1;
   printf("rows[%d]=>%f\n", last, rows[last].open);

   end = clock();
   printf("time=%f\n", (double)(end - start) / CLOCKS_PER_SEC); // 速度跟fscanf 差不多
   return 0;
}

相关资料:

C语言读取写入CSV文件之读取CSV文件

https://stdin.top/posts/csv-in-c/

https://stackoverflow.com/questions/12911299/read-csv-file-in-c

https://stackoverflow.com/questions/20013693/read-csv-file-to-a-2d-array-on-c

posted @   C羽言  阅读(937)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示