Extracting parts of string separated by space character

Hi,

I have a file with several lines, and these are it's contents:

1234561234561 aspirin 85 500 18/5/2012
1231234564567 cure 50 200 19/5/2012
1115559997775 voda 200 500 12/3/2012
1119997773335 andol 50 200 19/2/2012
7897894564561 fukitol 25 500 1/3/2012

These lines are in the format: code, cure name, quantity, price, date.
Now what I need is when the user, say, enters a certain number of cures, that quantity is increased or decreased, depending on the chosen option. And THIS is not the problem, because I can write the functions for these things.
What I DO have the problem with is the extraction of a third and fourth substring from a line. I'd then convert these numeric strings to int values so I could operate on them. Then, I'd need to change the line.

How can I do that? Thanks.
http://www.cplusplus.com/reference/string/string/find/
If you are using this as a database, I highly suggest using SQLite or MySQL.
The simple way is to extract the line piece by piece:
1
2
3
4
5
// File contains "String 123 anotherString"
string firstWord, secondWord;
int number;

input >> firstWord >> number >> secondWord;


Another way is to make use of find_first_of():
http://www.cplusplus.com/reference/string/string/find_first_of/
The example code there is exactly the sort of thing you need to do to parse the data.

Once the numbers are separated, you need some sort of atoi(). I only know with c-strings:
1
2
3
4
#include <stdlib.h> //  I believe nessesary

string word = "1234";
int num = atoi(c_str(word));
Last edited on
Topic archived. No new replies allowed.