在C中,如何从起始索引开始搜索char*中的子串?

huangapple go评论63阅读模式
英文:

How do I search for a substring in a char* from a starting index using C?

问题

I have the following loop which calls the parseXML() function:

...

char buffer[MAX_BUFSIZE];
memset(buffer, 0, sizeof(buffer));
int bytes_received = tcp_read(socket, buffer, sizeof(buffer));

...

int bytes_read_total = 0;
while (bytes_read_total < bytes_received) {
        
    int bytes_read = 0;
    Person* person = parseXML(buffer, bytes_received - bytes_read_total, &bytes_read);

    if (person == NULL) break;

    bytes_read_total += bytes_read;
    savePerson(person);

}

and I here is the start of my parseXML() function:

Person* parseXML(char* buffer, int bufSize, int* bytesRead) {
    
    char* startTag = strstr(buffer, "<person>");
    char* endTag = strstr(buffer, "</person>");
    if (startTag == NULL || endTag == NULL) {
        return NULL;
    }

    Person* person = newPerson();
    
    int personLength = endTag + strlen("</person>") - startTag;

    char* personBuffer = (char*)malloc(personLength + 1);
    memcpy(personBuffer, startTag, personLength);
    personBuffer[personLength] = '
Person* parseXML(char* buffer, int bufSize, int* bytesRead) {
    
    char* startTag = strstr(buffer, "<person>");
    char* endTag = strstr(buffer, "</person>");
    if (startTag == NULL || endTag == NULL) {
        return NULL;
    }

    Person* person = newPerson();
    
    int personLength = endTag + strlen("</person>") - startTag;

    char* personBuffer = (char*)malloc(personLength + 1);
    memcpy(personBuffer, startTag, personLength);
    personBuffer[personLength] = '\0';

    ...

    free(personBuffer);
    *bytesRead = personLength + 1;
    return person;
'
;
... free(personBuffer); *bytesRead = personLength + 1; return person;

When using strstr() currently it is always finding the first person in my XML each iteration instead of starting the search from the bytesRead offset. Please let me know how I can fix this.

英文:

I have the following loop which calls the parseXML() function:

...

char buffer[MAX_BUFSIZE];
memset(buffer, 0, sizeof(buffer));
int bytes_received = tcp_read(socket, buffer, sizeof(buffer));

...

int bytes_read_total = 0;
while (bytes_read_total &lt; bytes_received) {
        
    int bytes_read = 0;
    Person* person = parseXML(buffer, bytes_received - bytes_read_total, &amp;bytes_read);

    if (person == NULL) break;

    bytes_read_total += bytes_read;
    savePerson(person);

}

and I here is the start of my parseXML() function:

Person* parseXML(char* buffer, int bufSize, int* bytesRead) {
    
    char* startTag = strstr(buffer, &quot;&lt;person&gt;&quot;);
    char* endTag = strstr(buffer, &quot;&lt;/person&gt;&quot;);
    if (startTag == NULL || endTag == NULL) {
        return NULL;
    }

    Person* person = newPerson();
    
    int personLength = endTag + strlen(&quot;&lt;/person&gt;&quot;) - startTag;

    char* personBuffer = (char*)malloc(personLength + 1);
    memcpy(personBuffer, startTag, personLength);
    personBuffer[personLength] = &#39;
Person* parseXML(char* buffer, int bufSize, int* bytesRead) {
char* startTag = strstr(buffer, &quot;&lt;person&gt;&quot;);
char* endTag = strstr(buffer, &quot;&lt;/person&gt;&quot;);
if (startTag == NULL || endTag == NULL) {
return NULL;
}
Person* person = newPerson();
int personLength = endTag + strlen(&quot;&lt;/person&gt;&quot;) - startTag;
char* personBuffer = (char*)malloc(personLength + 1);
memcpy(personBuffer, startTag, personLength);
personBuffer[personLength] = &#39;\0&#39;;
...
free(personBuffer);
*bytesRead = personLength + 1;
return person;
&#39;; ... free(personBuffer); *bytesRead = personLength + 1; return person;

When using strstr() currently it is always finding the first person in my xml each iteration instead of starting the search from the bytesRead offset. Please let me know how I can fix this.

答案1

得分: 0

strstr函数的第一个参数接受一个字符指针,并且在遇到空字符(字符串结尾)时停止搜索。

指针可以用于算术运算,例如:

    int nums[2] = {1, 2};
    int *secondnum = nums + 1;  // “nums”数组被转换为指针
    printf("%d\n", *secondnum);  // 输出“2”

因此,要从偏移位置开始使用strstr搜索字符串,你只需增加指针:

    char *haystack = "foo 1, foo 2";
    char *needle = "foo";

    char *first_foo = strstr(haystack, needle);
    char *second_foo = strstr(first_foo + strlen(needle), needle);
    printf("%td\n", second_foo - haystack);  // 输出“7”,第二个“foo”的位置

strstr找不到子字符串时,它返回一个NULL指针。你可以通过以下方式检查是否找到了子字符串:

    char *found = strstr(haystack, needle);
    if (found != NULL) {
        // found是指向子字符串位置的指针
    } else {
        // 未找到子字符串
    }
英文:

strstr accepts a char pointer as its first parameter, and stops searching when it reaches a null character (end-of-string).

Pointers can be used in arithmetic, for example:

    int nums[2] = {1, 2};
    int *secondnum = nums + 1;  // &quot;nums&quot; array decays (turns) into a pointer
    printf(&quot;%d\n&quot;, *secondnum);  // prints &quot;2&quot;

As such, to search a string starting from an offset using strstr, all you need to do is increment your pointer:

    char *haystack = &quot;foo 1, foo 2&quot;;
    char *needle = &quot;foo&quot;;

    char *first_foo = strstr(haystack, needle);
    char *second_foo = strstr(first_foo + strlen(needle), needle);
    printf(&quot;%td\n&quot;, second_foo - haystack);  // prints &quot;7&quot;, the position of the second &quot;foo&quot;

When strstr doesn't find the substring, it returns a NULL pointer. You can check if the substring was found doing the following:

    char *found = strstr(haystack, needle);
    if (found != NULL) {
        // found is a pointer to the position of the substring
    } else {
        // substring not found
    }

huangapple
  • 本文由 发表于 2023年5月23日 00:10:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定