如何获取存储在双引号中的值

how to get value stored within double inverted commas

char string[200]="ret=\"y\"  err=\"000\"";
    char stringg[50];
    char *arg;
    arg  = strstr (string, "ret");
    memcpy(stringg,arg+5,1);
    printf(stringg);

我想复制 "ret" 的值,上面的程序给出了输出,但是当 ret 的值发生变化时,我必须在程序中进行更改。如何解决这个问题

在你的代码中,他们不需要给你的数组限制 string。您可以简单地使用:

const char string[] = "ret=\"y\"  err=\"000\"";

strstr()找到"ret"的值后,检查没有返回NULL,可以将所有内容复制到单独的数组或指针中,然后停止复制当找到第一个 space 时。这将确保 ret = "y" 被复制。找到 space 后,使用 [=17=] 终止符终止数组或指针。

然后可以用strchr(3)找到'='字符的位置,如果存在,就把它后面的值赋给另一个变量。然后,该变量将包含等号后的值。

这是一个例子:

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

int main(void) {
    const char string[] = "ret=\"y\"  err=\"000\"";
    const char *key = "ret";
    const char equals = '=', space = ' ', quote = '"';
    char *start, *copy, *next, *value;
    size_t i;

    start = strstr(string, key);
    if (start != NULL) {
        copy = strdup(start);
        if (copy == NULL) {
            fprintf(stderr, "Failed to copy contents of string\n");
            exit(EXIT_FAILURE);
        }
        for (i = 0; start[i] != space && start[i] != '[=11=]'; i++) {
            copy[i] = start[i];
        }
        copy[i] = '[=11=]';

        next = strchr(copy, equals);
        if (next != NULL) {
            next++;
            if (*next == quote) {
                next++;
                next[strlen(next)-1] = '[=11=]';
                value = next;
            }
        }

        printf("%s = %s\n", key, value);

        free(copy);
    }

    return 0;
}