Splitting String Help

Hi, I have this string here:


"
Membership information
----------------------
Nodeid Votes Name
1 1 Server1
2 1 Server2 (local)

"
How can I read it line by line and store the node ids and names in an array?
Last edited on
Hi,
> How can I read it line by line and store the node ids and names in an array?
What do you have in your program now? What have you gotten so far?
closed account (E0p9LyTq)
Use a string string to read your string, if you have carriage returns/end-of-line characters ('\n') delimiting each line. Instead of using std::cin, use a std::istringstream as your input source.
http://www.cplusplus.com/reference/sstream/istringstream/
I do not know if there are any \n to skip lines because I am using sshpass to ssh into a linux machine to run a command and retrieve the output as a string
closed account (E0p9LyTq)
If there are typical input delimiters that would cause std::cin to retrieve partial input from the stream, such as spaces, then a string stream will work the same way. A stream in C++ is a stream no matter where it comes from. The key thing is knowing the exact nature of the data string you are trying to muck with.
Try :
1
2
3
4
5
6
7
8
9
10
11
int id1, id2;
std::string server_name;
std::stringstream ss ("1 1 Server1\n" "2 1 Server2");

cout << "Output : " << endl;
while(ss >> id1 >> id2 >> server_name)
{
    cout << "id1 == " << id1 << endl;
    cout << "id2 == " << id2 << endl;
    cout << "server name == " << server_name << endl;
}
Topic archived. No new replies allowed.