编辑代码

#include <iostream>
#include <string.h>

char* read_string(char *rec, char **ssid, char **passwd) {
    char *p1 = strstr(rec, "ssid:");
    char *p2 = strstr(rec, "passwd:");
    if (p1 == NULL || p2 == NULL) {
        return "Invalid format";
    }
    p1 += 5; // move pointer to the beginning of ssid value
    p2 += 7; // move pointer to the beginning of passwd value
    *ssid = p1;
    *passwd = p2;
    // find the end of ssid and passwd values
    char *end1 = strstr(p1, ",");
    char *end2 = strstr(p2, ",");
    if (end1 == NULL || end2 == NULL) {
        return "Invalid format";
    }
    // terminate ssid and passwd string by replacing , with \0
    *end1 = '\0';
    *end2 = '\0';
    return "Success";
}

int main() {
    char rec[] = "ssid: my_wifi, passwd: my_password";
    char *ssid, *passwd;
    char *status = read_string(rec, &ssid, &passwd);
    if (strcmp(status, "Success") == 0) {
        printf("ssid: %s\n", ssid);
        printf("password: %s\n", passwd);
    } else {
        printf("Error: %s\n", status);
    }
    return 0;
}