gpt4 book ai didi

c - sscanf 多字符串扫描

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:00 27 4
gpt4 key购买 nike

我想用 sscanf 在 c 中捕获以下字符串

"1=Salam Khobi|FC93F8A120F491F3A8=Rial|F191FEA4"

但 sscanf 只用“Salam Khobi”填充 &customInput.typecustomInputTitle[0],而不会扫描字符串的其他部分。

 #include <stdio.h>
#include <stdlib.h>

typedef enum {
INPUT_NUMBER = 0,
INPUT_NORMAL = 1,
INPUT_PASSWORD = 2,
INPUT_PAYAMOUNT = 3,
} inputType;

typedef struct {
char * title[2];
char * extra[2];
inputType type;
unsigned minLen:6;
unsigned maxLen:6;
unsigned forceLen:1;
unsigned editable:1;
unsigned char data[100];
} lcdInput;
#define CUSTOM_INPUT_LENGTH 40
static unsigned char customInputTitle[2][CUSTOM_INPUT_LENGTH];
static unsigned char customInputExtra[2][CUSTOM_INPUT_LENGTH];
const char * payload = "1=Salam Khobi|FC93F8A120F491F3A8=Rial|F191FEA4";
#define CUSTOM_INPUT_REGX "%d=%[^|]s|%[^=]s=%[^|]s|%s"
static lcdInput customInput = {
.title = {&customInputTitle[0], &customInputTitle[1]},
.extra = {&customInputExtra[0], &customInputExtra[1]},
.type = INPUT_NORMAL,
.editable = 1,
.forceLen = 0,
};

int main()
{
memset(&customInputTitle, 0, CUSTOM_INPUT_LENGTH << 1);
memset(&customInputExtra, 0, CUSTOM_INPUT_LENGTH << 1);

sscanf(payload, CUSTOM_INPUT_REGX,
&customInput.type,
&customInputTitle[0], &customInputTitle[1],
&customInputExtra[0], &customInputExtra[1]);

return 0;
}

最佳答案

"%d=%[^|]|%[^=]=%[^|]|%s" 是正确的格式。

关于c - sscanf 多字符串扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53850625/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com