Read each line from the file into a string.
Then parse out the parts of the line.
The easiest way to do this is with stringstream.
1 2 3 4 5 6 7 8 9 10 11 12
string line;
while (getline(file,line))
{ stringstream ss (line);
string junk;
int val;
while (getline (ss, junk, ','))
{ // Got a label
ss >> val; // read in the int
// Do something with the value
getline (ss, junk, ';'); // flush the trailing ;
}
}
Something like this takes in a lot of memory. I am trying to read from a file and the size is quite big. so line could contain 100 inputs. Is there a way in which it searches in the input and just stores the numbers.
Huh? The code I posted above allocates one stringstream object and two string objects. That's not very much memory. The memory (except line) is going to be released at the bottom of the loop. line will be only as big as the longest line (a few hundred bytes).
I am trying to read from a file and the size is quite big
The code I posted takes a small amount of memory, irrespective of the size of the file. What you do with each value read, now that is a different matter.
so line could contain 100 inputs.
So?
Is there a way in which it searches in the input and just stores the numbers.
There are other ways to parse the lines. None as simple as what I posted.