69,336
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int look_for_keyword(char **pStandbyData, int *npStandbyDataLen, char *keyword);
int main(int argc, const char *argv[])
{
char *source="content-Disposition: form-data; name=\"subType\"\
\
1\
-----------------------------7db2221ba0d2e";
int length = strlen(source);
printf("%d\n", length);
look_for_keyword(&source, &length, "subType"); //查找关键字subType后的内容
printf("%s\n", source);
return 0;
}
static int look_for_keyword(char **pStandbyData, int *npStandbyDataLen, char *middle_keyword)
{
int length = 0; //记录full_keyword的长度
char *p = *pStandbyData; //指针p指向串头
char *content = NULL; //content保存截取的内容
content = (char *)malloc(sizeof(char) * 1024); //分配内存
memset(content, '\0', 1024);
char *q = content; //指针q指向content串头
char *head_keyword = "name=\""; //查找关键字头
char *full_keyword = NULL; //需要查找的关键字
full_keyword = (char *)malloc(sizeof(char) * 80); //分配内存
memset(full_keyword, '\0', 80); //初始化
strcpy(full_keyword, head_keyword);
strcat(full_keyword, middle_keyword);
length = strlen(full_keyword);
p = strstr(*pStandbyData, full_keyword); //p指向查找的关键字头
p += length; //p指向要截取的内容头
while(*p != '-') {
*q = *p;
p++;
q++;
}
*q= '\0';
memset(*pStandbyData, '\0', *npStandbyDataLen);
*npStandbyDataLen = a;
strcpy(*pStandbyData, content);
free(content);
free(full_keyword);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char *p;
char *source="content-Disposition: form-data; name=\"subType\"1 -----------------------------7db2221ba0d2e";
p=strstr(source,"subType");
if (p) printf("%s\n",p+7);//subType后的内容即"1 -----------------------------7db2221ba0d2e
return 0;
}