英文:
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 < 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.
答案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; // "nums" array decays (turns) into a pointer
printf("%d\n", *secondnum); // prints "2"
As such, to search a string starting from an offset using strstr
, all you need to do is increment your pointer:
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); // prints "7", the position of the second "foo"
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论