Read different types from a getline string

Hi all,

What is the best way of reading in a line at a time from a file, and extracting a char followed by two doubles from it?

Here is some context:
In my program, I want to open a file containing a list of numbers which represent complex numbers, like this:

p 100. 0.80
r 50. 50.
p 20 4.8
r -100. 25


The first character represents whether it is in rectangular or polar form, and the two (double) numbers are part1 and part2 respectively.

This is my code at the moment, which works (Complex is my complex number class):

1
2
3
4
5
6
7
8
9
10
11
12
13
char form(' ');
double part1(0.0), part2(0.0);
string temp;
Complex cn;

while ( !complex_number_file.eof() ) {
	getline( complex_number_file, temp );
	if ( temp == "" ) continue;
	stringstream(temp) >> form >> part1 >> part2;
	cn.set_data(form, part1, part2);
	cn.print_rectangular_form();
	cn.print_polar_form();
}


Is there a better way of doing this? Using stringstream just seems clumsy and unnecessary to me.
Also, is there any reason to use getline() at all? Or should I just use:

complex_number_file >> form >> part1 >> part2;
You'd probably be fine with extracting the char, then each number straight from the file stream. After all, the operator will skip whitespace.
Topic archived. No new replies allowed.