C++字符串模式匹配缓冲区数据
我有一个脚本中的入站缓冲区数据,我需要使用key=>'value'来运行一个数学方程。是的,我知道我需要转换为int。因为我确定数据是字符串,所以我尝试对它运行模式匹配。 我看到了入站数据,但从未得到正匹配 代码:C++字符串模式匹配缓冲区数据,c++,regex,strstr,memcmp,C++,Regex,Strstr,Memcmp,我有一个脚本中的入站缓冲区数据,我需要使用key=>'value'来运行一个数学方程。是的,我知道我需要转换为int。因为我确定数据是字符串,所以我尝试对它运行模式匹配。 我看到了入站数据,但从未得到正匹配 代码: 我如何得到[total]并将3添加到其中?[total]+3?您只是将buff的前5个字节与total匹配,而不是实际搜索。如果缓冲区不包含任何空值,则要使用的函数为: 如果您的缓冲区可以包含空值,那么您需要编写一个名为memstr的函数,这非常简单:只需尝试在每个点找到它: con
我如何得到[total]并将3添加到其中?[total]+3?您只是将buff的前5个字节与total匹配,而不是实际搜索。如果缓冲区不包含任何空值,则要使用的函数为: 如果您的缓冲区可以包含空值,那么您需要编写一个名为memstr的函数,这非常简单:只需尝试在每个点找到它:
const char* memstr(const char* str, size_t str_size,
const char* target, size_t target_size) {
for (size_t i = 0; i != str_size - target_size; ++i) {
if (!memcmp(str + i, target, target_size)) {
return str + i;
}
}
return NULL;
}
在您的案例中,其用法是:
const char* total = memstr(buff, BUFSIZ, key, sizeof(key) - 1);
你能详细说明一下这个[总数]+3是什么意思吗。
while (fgets( buff, BUFSIZ, fp)) {
const char* total = strstr(buff, key);
if (total) {
// found our total, which should point
// ["total"] =>
// ^
// here
}
}
const char* memstr(const char* str, size_t str_size,
const char* target, size_t target_size) {
for (size_t i = 0; i != str_size - target_size; ++i) {
if (!memcmp(str + i, target, target_size)) {
return str + i;
}
}
return NULL;
}
const char* total = memstr(buff, BUFSIZ, key, sizeof(key) - 1);