Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Read in file lines separated by a comma

I tried looking up what I'm trying to do but I cant find specifically what I'm trying to do. I have a text file with multiple lines that look like this:

12345,12345,12.34,12345,12345

It's the same format on every line and I want to get each line and plug the numbers into certain variables. Something like this:

file >> int1 >> int2 >> double1 >> int3 >> int4;

But this is very hard for me to do because of the comma separating each number. I used to be able to do this when there was a 'space' but the comma is really throwing me off. Any ideas?

like image 512
Arubix Avatar asked Dec 27 '22 06:12

Arubix


2 Answers

char ch;
file >> int1 >> ch >> int2 >> ch >> dbl >> ch >> int3 >> ch >> int4;
like image 163
gongzhitaao Avatar answered Dec 31 '22 14:12

gongzhitaao


You may want to try fscanf.

Something like this?

 fscanf(filepointer, "%d,%d,%f,%d,%d\n", &int1, &int2, &double1, &int3, &int4);
like image 21
frsfnrrg Avatar answered Dec 31 '22 13:12

frsfnrrg