Question

I store list of my students in a text file. Every Student's primary data is stored in one line and list of his classes as second line, where classes are separated by ','. It goes like Mathematics,Linear Algebra,Physical Education Adv, Optics,. If i read this to one string, how can i divide it, so temp1 will get Mathematics, temp2 Linear Algera and so on...?

Was it helpful?

Solution

Use strtok function - Use this page for reference http://www.cplusplus.com/reference/cstring/strtok/

Though it might look difficult to use at first, but it's quite efficient

OTHER TIPS

In case you need a robust ready-to-go function that works with std::string and std::vector:

using namespace std;
vector<string> splitString(const string &str, const string &delim)
{
    size_t start = 0, delimPos = 0;
    vector<string> result;
    do
    {
        delimPos = str.find(delim, start);
        result.push_back(string(str, start, delimPos-start));
        start = delimPos + delim.length();
    } while(delimPos != string::npos);
    return result;
}

I actually pulled this out of my snipped library ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top