Reading "*" with ifstream

71 views Asked by At

I am trying to read the columns from a text file which look like

name number1*number2

why something like

Float_t value=0;
ifstream ifs("values.dat");
string line;
while(std::getline(ifs, line)) // read one line from ifs
{
    istringstream iss(line); // access line as a stream
    string dataname;
    ifs >> dataname >> value; // no need to read further

but it fails to read the "*number2" part ie it passes to xs only the number1 value. How can I fix this ?

1

There are 1 answers

0
Sergey Kalinichenko On

In the absence of whitespace around the operator you can read the asterisk as a single char:

int number1, number2;
char op;
iss >> number1 >> op >> number2;

Demo.