samedi 25 avril 2015

How to Find all Occurances of a Substring in C

I am trying to write a parsing program in C that will take certain segments of text from an HTML document. To do this, I need to find every instance of the substring "name": in the document; however, the C function strstr only finds the first instance of a substring. I cannot find a function that finds anything beyond the first instance, and I have considered deleting each substring after I find it so that strstr will return the next one. I cannot get either of these approaches to work. Any suggestions? Thanks.

By the way, I know the while loop limits this to six iterations, but I was just testing this to see if I could get the function to work in the first place.

                    while(entry_count < 6)
                    {   
                        printf("test");
                        if((ptr = strstr(buffer, "\"name\":")) != NULL)
                        {   
                            ptr += 8;
                            int i = 0;
                            while(*ptr != '\"')
                            {   
                                company_name[i] = *ptr;
                                ptr++;
                                i++;
                            }   
                            company_name[i] = '\n';
                            int j;
                            for(j = 0; company_name[j] != '\n'; j++)
                                printf("%c", company_name[j]);
                            printf("\n");
                            strtok(buffer, "\"name\":");
                            entry_count++;
                        }   
                    }   

Aucun commentaire:

Enregistrer un commentaire