如何将csv文件读入结构数组 [英] How to read csv file into structure array

查看:112
本文介绍了如何将csv文件读入结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将.csv文件读入结构数组?我能够将.txt文件读入结构数组。



尝试使用getline但它不起作用。





  void  loadsn(SN_curve sncurve [ 6 ])
{
ifstream infile;
int totalData = 0 ;
string value ;
infile.open( Curve.txt);

if (infile.is_open())
{

执行 {

infile>> sncurve [totalData] .material>> sncurve [totalData] .SFP>> sncurve [totalData] .slope>> sncurve [totalData] .elast_mod>> sncurve [totalData] .poisson>> sncurve [totalData] .km;

totalData ++;

} while (!infile.eof());

infile.close();
}

}





curve.csv文件。



焊接连接R1,48.5,4,1000,0.3,0.0,

焊接连接R2,234,5,2000,0.3,0.0

解决方案

您最好读取文件的每一行,并将整行解析为正确的值。



写一行解析函数。



我会为每一行创建一个类。

How to read .csv file into structure array? I am able to read .txt file into structure array.

tried with getline but it doesn't work.


void loadsn(SN_curve sncurve[6])
 {
	ifstream infile;
	int totalData = 0;
	string value;
             infile.open("Curve.txt");

   if (infile.is_open())
   {

      do {

        infile >>sncurve[totalData].material>>sncurve[totalData].SFP >> sncurve	            [totalData].slope>>sncurve[totalData].elast_mod >>sncurve[totalData].poisson >>sncurve	            [totalData].km;
		  
             totalData++;

          } while (!infile.eof());

             infile.close();
   }

}



curve.csv file.

Weld join R1 , 48.5,4 ,1000, 0.3 ,0.0,
Weld join R2 , 234, 5, 2000, 0.3,0.0

解决方案

you better read each line of the file and parse the complete line to the right values.

Write a line parsing function.

I would create a class for each line.


这篇关于如何将csv文件读入结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆