Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C++
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
Posted
Updated 5-May-14 10:26am
v2
Comments
José Amílcar Casimiro 5-May-14 16:19pm    
Read: http://www.codeproject.com/Questions/306431/Parsing-CSV-Files-By-Cplusplus
Maciej Los 5-May-14 16:29pm    
Good idea!
José Amílcar Casimiro 5-May-14 17:00pm    
:)
Richard MacCutchan 6-May-14 3:23am    
iostream does not recognise the comma separators. It would be better to read the entire line and then use a tokeniser to split the data into the separate fields.
Ashish Tyagi 40 6-May-14 9:04am    
You are writing binary data in .txt file without any delimiter aand line break, and .csv files are string data having (,) comma (mostly) as delimiter and line break to separate objects, so you need new reading logic in loadsn function.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900