0

i need some help: i have something like:
word1:word2:word3:word4 ...read from a file,how can i do,when i read this line,each word delimit by ":" to put into a string....
string str1,str2,str3,str4;
str1 = word1;
...........
str4 = word4;

sorry for my english i hope eu understand

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by daviddoria
0

You can use getline to read from a stringstream buffer upto a delimiter, then push each word into a vector (a C++ array)

#include <string>
#include <vector>
#include <sstream>

int main()
{
    std::string line = "word1:word2:word3:word4";
    std::istringstream buffer(line);

    std::vector<std::string> words;
    std::string word;

    while( std::getline(buffer, word, ':') )
        words.push_back(word);
}

Edited by Bench: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.