how to use get() ?

Feb 2, 2009 at 6:33pm
I have managed to break down line by line of a file containing several lines of datas. Using

string x;
getline(abc.txt,x);

however, if i would like to further break down the string x (as it contains "1 4 12 54") into each integer and store each of them into an array. What should i do? I tried to use get(). But don't really know how to use. What header file should i include?

many thanks for help.
Feb 2, 2009 at 6:46pm
istream::get() reads the next character from a stream
eg:
1
2
3
char c;
cin.get(c);
c = cin.get();
both lines 2 and 3 will set 'c' to the value of the next character the user input
http://www.cplusplus.com/reference/iostream/istream/get.html

The thing you want to do with strings and numbers could be done with stringstreams
eg:
1
2
3
4
string x = "1 4 12 54";
int n[4];
stringstream ss(x);
ss >> n[0];//n[0] now == 1 
http://www.cplusplus.com/reference/iostream/stringstream/

This article may be useful: http://www.cplusplus.com/forum/articles/6046/ (you can replace cin with any istream)
Last edited on Feb 2, 2009 at 6:52pm
Feb 3, 2009 at 1:40am
THX Bazzy !!!
Topic archived. No new replies allowed.