Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all I'm working with Parsing a CSV file with c++, I have successfully parsed the data but now I'm trying to convert each column to its appropriate type as I have declared a-j. I have it just saying somenumbers dd.a = stod(vector[0]) for example I'm hoping this is valid cause the data will be sent to a csv file continuously so there can be n number of rows but I know there is only 10 columns. I just want to make sure that by doing what I'm doing that each variable will gets its data after each scan? and no row of data will be skipped?

	typedef struct {
		int a=0;
		int b=0;
		int c=0;
		double d=0;
		double e=0;
		double f=0;
		double g=0;
		int h=0;
		double i=0;
		double j=0;
	}somenumbers;

	vector<vector<string> > matrix;
	somenumbers dd;
	string line;
	ifstream file("mynumbers - Copy.csv");
	
	if (!file.is_open())
	perror("error while opening file");
	
	
	getline(file, line, '\n');

	while (getline(file, line, '\n')) //find the endline charcater
	{

		vector<string> vec;
		string linevec;
		istringstream ss(line);//allows for parsing each line 

		while (getline(ss, linevec, ','))
		{

			vec.push_back(linevec);//push the parsed data for that line into a vector

		}

		
		dd.a= stod(vec[0]);
		dd.b = stod(vec[1]);
		cout << dd.a << endl;
		
		matrix.emplace_back(vec);//push each parsed line into another vector
		
		
}


What I have tried:

I have successfully got all the end character and commas
the csv file is formatted as follow
a,b,c,d,e,f,g,h,i,j
1.,
.
.
.
.
n., rows
Posted
Updated 26-Mar-17 15:43pm

Its easy enough to test your loop(s) to

get a line
split a line into comma separated 'variables'

- I'd concentrate on that first ... this though

getline(file, line, '\n');

  while (getline(file, line, '\n')) //find the endline charcater


ok, so, you're going to get a line, immediately discard it and get another line (in the while) - is there a reason for discarding the first line ? I understand if its a 'header' line, but then, you may wish to comment that fact in the code, so that anyone else reading gets the 'why'

this code - Im presuming is just testing

dd.a= stod(vec[0]);
       dd.b = stod(vec[1]);


you wouldnt really hard code all the way to vec[9] would you ? you would use a FOR loop for if you were 100% sure there was only ever going to be 10 columns, or, use an iterator on 'vec' - personally, I would use the iterator, keep a count, and if you end up with more than 10 columns stop the run

[edit] ok, I looked back and saw this

typedef struct {
      int a=0;
      int b=0;
      int c=0;
      double d=0;
       double e=0;
       double f=0;
       double g=0;
       int h=0;
      double i=0;
       double j=0;
   }somenumbers;


is it your intent to parse each line of the 'csv' file into a 'somenumbers' struct ? if so, everything I said still applies - in that case I'd probably still use an iterator/counter to assign from the vec[] to somenumbers, as in

C#
...
switch(fieldNumber) :
case 0 : a = convertToInt(vec[fieldNumber]);   
case 1 : b = convertToInt(vec[fieldNumber]);   
...


where convertToInt is some function you have to convert from a string to an Int, you'd also need a convertToDouble I guess

[/edit]
 
Share this answer
 
v4
Comments
musclesmania05 14-Apr-17 2:12am    
I'm not sure how to use the vector iterator when you have a struct of different types ?? if so please explain or show?
Yes the reason for the getline on the first Line is cause that the header line.

Yes I was testing an hoping for a better way I'm not to sure how to use the iterator on the vec can you explain?
and also I'm sticking vec into matrix as well so would I use iterator on matrix instead?
Last yes the "struct somenumbers" relates to the column types so that why that's there so when I see column 1 I know int ... and so on.
 
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