#include <stdlib.h>
#include<stdio.h>
#include <string.h>
#define ROWS 6
#define COLS 3
//====================================================================//
void my_readcsv(char *path)
{
FILE *fp = NULL;
errno_t err;
err = fopen_s(&fp, path, "r");
if (err == 0)
{
printf("The file %s' was opened\n\n", path);
}
else
{
printf("The file %s' was not opened\n\n", path);
}
double arr[ROWS][COLS];
char d;
int ret=1;
int i = 0;
while ( ret != EOF && i < ROWS )
{
for(int j=0; j<COLS; j++)
{
ret = fscanf( fp, "%lf", &arr[i][j]);
ret = fscanf( fp, "%c", &d);
}
i = i + 1;
}
err = fclose(fp);
printf("Access elements in arr: \n");
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
printf("%f ", arr[i][j]);
}
printf("\n");
}
}
//====================================================================//
int main()
{
char path[260];
strcpy(path, "D:/A.csv");
my_readcsv(path);
return 0;
}
运行结果:
The file D:/A.csv' was opened
Access elements in arr:
0.051086 0.026424 0.065972
0.068504 0.023359 0.069607
0.043467 0.031274 0.049570
0.116308 0.037569 0.041143
0.057333 0.030825 0.046375
0.085318 0.033363 0.037360
原CSV表格: