infinite loop?

Jul 15, 2014 at 2:54pm
Hi.
Why cannot I find the number of occurrences of "<context>" in the character string held by char buffer[12000] and it seems to fall into infinite loop?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    char *pc1;
    char *pc2;
    
    pc1 = strstr(buffer, "<context>");  //searching for the first occurrence of <context>
    pc2 = strstr(buffer, "</context>");
    
//   int contextsNo++;
    
    pc1 +=10;
    
    while(pc1 != 0)
    {
              pc1 = strstr(pc1, "<context>");  //searching for the next occurrence of <context>
              cout << "1" << endl;
           //   ++contextsNo;
    }
Jul 15, 2014 at 4:06pm
strstr returns a pointer to the first character in the substring so if you start searching from the returned pointer position you will find the same string again, and again .... You probably should continue the search from a postion further forward.
Jul 15, 2014 at 4:52pm
Yes Peter, I did it, and it worked!Thanks!
Topic archived. No new replies allowed.