最近在实现一些算法时,需要从csv里读数据并进行测试,所以需要一个读数据的接口,故在此记录一下下。
参考资料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;
}
结果如下: