446,331 Members | 1,454 Online Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

# strtok question

 P: 10 If I understand it correctly, strtok scans from the first character after the separator. The code below: Expand|Select|Wrap|Line Numbers #include  #include  using namespace std;   void main() {     char *rules;     int priority;     int i=0;     char line=" (arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)";       char *tok = strtok(line,"(");     rules[i]=strtok(NULL,",");       tok=strtok(NULL,")");     priority[i]=atoi(tok);     i++;       while((tok=strtok(NULL,"("))!=NULL)     {         rules[i]=strtok(NULL,",");         tok=strtok(NULL,")");         priority[i]=atoi(tok);         i++;     }       for(int j=0;j
8 Replies

 Expert 100+ P: 1,510 the first token is framed with (, so try Expand|Select|Wrap|Line Numbers     char line="(arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)";     char *tok;      rules[i]=strtok(line,"(,");   Mar 23 '07 #2

 P: 10 the first token is framed with (, so try Expand|Select|Wrap|Line Numbers     char line="(arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)";     char *tok;      rules[i]=strtok(line,"(,");   Thank you. That fixes the problem. I then modified the input into: Expand|Select|Wrap|Line Numbers char line="(arule, 12), (brule, 21), (zrule, 70), (drule, 25), (erule, 10)(frule, 3)(grule, 20), (srule, 100)"; Notice that there's no "," between "(erule, 10)", "(frule, 3)", and "(grule, 20)". What I want to do is, let the program read from "(arule, 12)" until "(erule,10)", pause right before "(frule,3)", do something, then continue reading "(frule, 3)", pause before "(grule, 20)", do something, then continue reading "(grule,20), (srule, 100)". So far I've tried adding: Expand|Select|Wrap|Line Numbers char next=")"; and then changing the while loop to: Expand|Select|Wrap|Line Numbers     while((num=strtok(NULL,"("))!= NULL && (num=strtok(NULL,"("))!=next)     {         rules[i]=strtok(NULL,",");         num=strtok(NULL,")");         priority[i]=atoi(num);         i++;     } but the output becomes: arule 12 zrule 70 erule 10 srule 100 I figured everytime a strtok is called it changes the beginning token so I then changed the while loop into: Expand|Select|Wrap|Line Numbers     while((num=strtok(NULL,"("))!= NULL)     {         rules[i]=strtok(NULL,",");         num=strtok(NULL,")");         priority[i]=atoi(num);         i++;           if(num==next)             cout<<"pause"; //to test if the condition works     } but the output becomes: arule 12 brule 21 zrule 70 drule 25 erule 10 grule 20 srule 100 What am I doing wrong? Thank you. Mar 24 '07 #3

 P: 10 Will someone be so kind to point out why my program hangs? It doesn't give out any error message other than "". Expand|Select|Wrap|Line Numbers #include  #include  using namespace std;   void main() {     ifstream infile;     char c,str;     char *rules;     int priority;     int i=0;     char *num;       cout<<"Enter the name of input file: ";     cin.get(str, 64);       infile.open(str);       for(i=0;i<256;i++)     {         c[i]=infile.get();         cout<

 Expert 100+ P: 1,510 your program works fine with GNU C++. The problem may be in your the loop where you read a line from the file Expand|Select|Wrap|Line Numbers     for(i=0;i<256;i++)     {         c[i]=infile.get();         cout<

 P: 10 Expand|Select|Wrap|Line Numbers infile.get(c,256);   so you read up to 256 characters or until a newline is found Thank you! I wasn't aware of the difference between c[i]=infile.get() and infile.get(c,256). Now the program can read the line just fine. However, it hangs again if the input is like this: (arule, 10), (brule,20)(crule, 30)(drule, 40), (erule, 50)(frule, 60) I use Visual Studio and it simply says the program (the one I wrote) has stopped working. The output would look like this: arule 10 brule 20 drule 40 erule 50 (crule, 30) and (frule, 60) are both missing. I think it's got something to do with how the delimiter is set up. What I am trying to do is, let the program read block "(arule, 10), (brule, 20)", do something, read (crule, 30), do something, read block "(drule, 40), (erule, 50)", do something, then read (frule, 60) and do something. So how do I make a condition that says if current token ends with ")" and there's a "(" immediately follows, then do something before continue reading next block? Thank you. Mar 25 '07 #6

 Expert 100+ P: 1,510 it gives a segmentation error with Visual; C++ on my machine the problem is if strtok() returns NULL within your loop - you need to check and if so exit the loop, e.g. Expand|Select|Wrap|Line Numbers     while((num=strtok(NULL,"("))!=NULL)     {         if((rules[i]=strtok(NULL,","))==NULL)break;         cout<

 P: 10 it gives a segmentation error with Visual; C++ on my machine the problem is if strtok() returns NULL within your loop - you need to check and if so exit the loop, e.g. Expand|Select|Wrap|Line Numbers     while((num=strtok(NULL,"("))!=NULL)     {         if((rules[i]=strtok(NULL,","))==NULL)break;         cout<

 P: 10 Alright I have modified my code into this: Expand|Select|Wrap|Line Numbers #include  #include  #include  using namespace std;   #define index 5   struct am{     string rule;     int priority; }set[index];   void printset(am amg) {     cout< 