C语言读取csv格式文件中的数据

最近在实现一些算法时,需要从csv里读数据并进行测试,所以需要一个读数据的接口,故在此记录一下下。

参考资料1:https://blog.csdn.net/qingzhuyuxian/article/details/79791699?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-0&spm=1001.2101.3001.4242

参考资料2: https://blog.csdn.net/eye_water/article/details/80551444

原数据大概长这样。

在这里插入图片描述

直接放代码。

//
//  main.c
//  NmrAlgoForZhang
//
//  Created by  云子谣 on 2021/3/1.
//


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

//-------------------------------------------------------------------
// these typedefs below are about to define some alias so that we could understand it easily.
//-------------------------------------------------------------------
typedef double data_type;
typedef double** data_2d ;
typedef double* data_1d;
typedef char* path;
typedef int data_size;
#define PRINT_INT(data_int) printf("%d\n", data_int)
#define PRINT_FLOAT(data_float) printf("%f\n", data_float)
#define PRINT_DOUBLE(data_double) printf("%lf ", data_double)
#define MAX_LINE_SIZE 1024



//-------------------------------------------------------------------
// these methods would be  implemented to read the data from files users provided.
//-------------------------------------------------------------------

/*
 * @brief this method is to build a 2d matrix dynamically without initializing values for it.
 * @param data_org which is expected should be declared by users before,
 * @param the row and column expected is the height and width of the matrix we will build.
 */
data_2d build_data(data_size row, data_size col)
{
    data_2d data_org;
    data_org = (data_2d)malloc(row * sizeof(data_type *));
    for(data_size i = 0; i < row; i++)
    {
        data_org[i] = (data_type*) malloc(col * sizeof(data_type));
    }
    return data_org;
}



/*
 * @brief this method should be called when users prefer to get the count of row.
 * @param file_path expected is set by users to make sure we could locate the data files correctly.
 */
data_size get_row_from_csv(path file_path)
{
    char line[1024];
    data_size i = 0;
    FILE* stream = fopen(file_path, "r");
    while(fgets(line, 1024, stream))
    {
        i++;
    }
    fclose(stream);
    return i;
}


/*
 * @brief this method should be called when users prefer to get the count of column.
 * @param file_path expected is set by users to make sure we could locate the data files correctly.
 */
data_size get_column_from_csv(path file_path)
{
    char line[1024];
    data_size i = 0;
    FILE* stream = fopen(file_path, "r");
    fgets(line, 1024, stream);
    char* token = strtok(line, ",");
    while(token){
            token = strtok(NULL, ",");
            i++;
        }
    fclose(stream);
    return i;
}


/*
 * @brief this method should be called when users prefer to read data from .csv files.
 * @param data expected should be declared by users before,
 * @param file_path expected is set by users to make sure we could locate the data files correctly.
 */
//TODO there are some bugs.
void read_data_from_csv(data_2d data, path file_path)
{
    FILE* stream = fopen(file_path, "r");
    char* ptr;
    char str_line[MAX_LINE_SIZE];
    data_size i, j;
//    if(data == NULL)
    if(stream)
    {
        data_size row = get_row_from_csv(file_path);
        data_size column = get_column_from_csv(file_path);
        for(i = 0; i < row; i++)
        {
            j = 0;
            if(fgets(str_line, MAX_LINE_SIZE, stream))
            {
                ptr = strtok(str_line, ",");
                while (ptr != NULL) {
                    data[i][j] = atoi(ptr);
                    j++;
                    ptr = strtok(NULL, ",");
                }
            }
        }
        //close the file
        fclose(stream);
    }
}


/*
 * @brief print the data
 */
void print_data(double ** data)
{
    data_size i;
    data_size j;
    for (i = 0; i < 4404; i++)
        {
            printf("Line %i :",i+1);
            for (j=0;j<65;j++)
            {
                PRINT_DOUBLE(data[i][j]);
            }
            printf("\n");
        }
}


/*
 * @brief this method should be called when users need to release the space.
 */
void delete_data_2d(data_2d data, data_size row, data_size column)
{
    for(data_size i = 0; i < row; i++)
    {
        free(data[i]);
    }
    free(data);
    data = NULL;
}


char file_path[] = "/Users/****/data.csv";

测试用例如下所示:

// test
int main(int argc, const char * argv[]) {
    data_size row = get_row_from_csv(file_path);
    data_size column = get_column_from_csv(file_path);
//    data_2d data_org;
    data_2d data = build_data(row, column);
    read_data_from_csv(data, file_path);
    
    print_data(data);
    
    delete_data_2d(data, row, column);
    return 0;
}

结果如下:
在这里插入图片描述

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值