Parsing input into tokens using different delimiters in C++ -


so trying parse input data , break data string tokens, problem is, need different delimiters on same line , on different lines, @ same time. here example of need parse:

input:

<15> algorithm [binary tree] analysis heap <1> [binary search tree] analysis complexity algorithm [2-3 tree] <5> tree [b+ tree] [binary tree] <8> graph clique tree <5> tree [full binary tree] [complete binary tree] <-1> 

so above input, need take parse numbers inside angle brackets using delimeter <> have done , works in code. need parse data on lines using both dilemeter " " individual words, , "[]" delimeter words inside brackets need have spaces included.

so have:

// create file-reading object ifstream fin; fin.open("input.txt"); // open file if (!fin.good()) return 1; // exit if file not found  // read each line of file while (!fin.eof()) { // read entire line memory char buf[maxchars]; fin.getline(buf, maxchars); cout << "the line i'm read is: "<<  buf << endl;  int n = 0; // for-loop index  // array store memory addresses of tokens in buf char* token[maxtokens] = {}; // initialize 0  // parse line <> delimited tokens first token[0] = strtok(buf, "<>"); // number tokens <> delimeters if (token[0]) {   token[1] = strtok(token[0], "[]"); //break tokens spaces   (n = 1; n <20; n++)   {     token[n]=strtok(null, " "); //break tokens brackets     if (!token[n]) break; // no more tokens   } }  // process (print) tokens (int = 0; < n; i++) // n = #of tokens   cout << "token[" << << "] = " << token[i] << endl; cout << endl; } 

and kind of output:

the line i'm read is: <15> token[0] = 15       
the line i'm read is: algorithm [binary tree] analysis heap token[0] = algorithm  token[1] = binary tree token[2] =  analysis heap  line i'm read is: <1> token[0] = 1  line i'm read is: [binary search tree] analysis token[0] = [binary search tree token[1] =  analysis  line i'm read is: complexity algorithm [2-3 tree] token[0] = complexity algorithm  token[1] = 2-3 tree  line i'm read is: <5> token[0] = 5  line i'm read is: tree [b+ tree] [binary tree] token[0] = tree  token[1] = b+ tree token[2] =   token[3] = binary tree  line i'm read is: <8> token[0] = 8  line i'm read is: graph clique tree token[0] = graph clique tree  line i'm read is: <5> token[0] = 5  line i'm read is: tree [full binary tree] token[0] = tree  token[1] = full binary tree  line i'm read is: [complete binary tree] token[0] = [complete binary tree  line i'm read is: <-1> token[0] = -1 

basically need like:

token[0] = 15
token[0] = algorithm
token[1] = binary tree
token[2] = analysis
token[3] = heap
, etc....


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -