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...?
            Asked
            
        
        
            Active
            
        
            Viewed 45 times
        
    0
            
            
         
    
    
        Jan Jurec
        
- 49
- 1
- 13
- 
                    Here you have explanation how to do this: http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – Ardel Apr 09 '14 at 17:14
- 
                    Thank you, this link is also helpful. – Jan Jurec Apr 09 '14 at 17:49
2 Answers
0
            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
 
    
    
        Atul
        
- 874
- 1
- 7
- 17
0
            
            
        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 ;)
 
    
    
        Marius
        
- 2,234
- 16
- 18