<>前言

csv是常用的数据文件格式,在R、Python、Matlab这些语言中,读取csv是一件很容易的事情,那用最经典的c如何来读取csv呢?

<>要读取的文件

<>读取的代码

下面就来看看代码吧
#include <iostream> #include <fstream> #include <iomanip> #include <sstream> #
include <string> using namespace std; int main() { int len_x=5921; double x[
len_x]; ifstream inFile("gene_new.CSV", ios::in); if (!inFile) { cout <<
"打开文件失败!" << endl; exit(1); } int i = 0; string line; string field; while (
getline(inFile, line))//getline(inFile, line)表示按行读取CSV文件中的数据 { string field;
istringstreamsin(line); //将整行字符串line读入到字符串流sin中 getline(sin, field, ',');
//将字符串流sin中的字符读入到field字符串中,以逗号为分隔符 // cout<<atof(field.c_str())<<"
";//将刚刚读取的字符串转换成int x[i]=atof(field.c_str()); i++; } inFile.close(); //
看看建立的数组好了没 for(i=0;i<len_x;i++) { cout<<"NO."<<i+1<<":"<<x[i]<<endl; } return 0;
}
<>结果

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信