C++读取CSV表格(double型数据)


#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表格:







  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值