繁体   English   中英

将char数组转换为类似于int argc和char ** argv的结构

[英]Converting a char array into an structure similar to int argc and char** argv

我已经从文件中读取了文本行,并且需要将其转换为类似于主函数​​参数的结构。 例如,如果char数组是char* text="There are books in the library." 而且我有以下结构定义:

struct mystruct{
   int argc;
   char** argv;
};

如果我使用函数foo(a, text)具有struct mystruct a ,则最终得到a.argc等于6, a.argv[0]等于Therea.argv[1]等于are ,... 。

CI中是否有任何功能或库可用于此目的? 由于执行C main函数时,输入参数会自动完成此转换。

您可以使用strtok进行拆分,如下所示:

struct split_result {
    int cnt;
    char *buf;
    char **strs;
};

int
split(const char *str, struct split_result *rst)
{
    int idx, str_num;
    char *buf, *sep, **strs;

    buf = strdup(str);
    if (buf == NULL) {
        perror("strdup");
        return -1;
    }

    str_num = 1;
    strs = malloc(str_num * sizeof(char *));
    if (strs == NULL) {
        perror("malloc");
        return -1;
    }

    sep = " \t";
    idx = 0;
    for (strs[idx] = strtok(buf, sep);
         strs[idx];
         strs[idx] = strtok(NULL, sep))
    {
        idx++;
        if (idx >= str_num) {
            str_num += 10;
            strs = realloc(strs, str_num * sizeof(char *));
            if (strs == NULL) {
                perror("realloc");
                return -1;
            }
        }
    }

    rst->cnt = idx;
    rst->strs = strs;
    rst->buf = buf;

    return 0;
}

这是一个测试程序及其输出:

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

struct split_result {
    int cnt;
    char *buf;
    char **strs;
};

int
split(const char *str, struct split_result *rst)
{
    int idx, str_num;
    char *buf, *sep, **strs;

    buf = strdup(str);
    if (buf == NULL) {
        perror("strdup");
        return -1;
    }

    str_num = 1;
    strs = malloc(str_num * sizeof(char *));
    if (strs == NULL) {
        perror("malloc");
        return -1;
    }

    sep = " \t";
    idx = 0;
    for (strs[idx] = strtok(buf, sep);
         strs[idx];
         strs[idx] = strtok(NULL, sep))
    {
        idx++;
        if (idx >= str_num) {
            str_num += 10;
            strs = realloc(strs, str_num * sizeof(char *));
            if (strs == NULL) {
                perror("realloc");
                return -1;
            }
        }
    }

    rst->cnt = idx;
    rst->strs = strs;
    rst->buf = buf;

    return 0;
}

int
main(void)
{
    int i, j;
    struct split_result rst;
    const char *msg[] = {
        "",
        " One",
        " One  Two",
        " One  Two Tree  ",
        "There are books in the library.",
        NULL,
    };

    for (i = 0; msg[i]; i++) {
        if (split(msg[i], &rst) < 0) {
            exit(EXIT_FAILURE);
        }

        printf("msg[%d] = >>%s<<\n", i, msg[i]);
        for (j = 0; j < rst.cnt; j++) {
            printf("cnt = %d: |%s|\n", j, rst.strs[j]);
        }

        free(rst.strs);
        free(rst.buf);
    }

    exit(EXIT_SUCCESS);
}

输出:

$ ./a.out 
msg[0] = >><<
msg[1] = >> One<<
cnt = 0: |One|
msg[2] = >> One  Two<<
cnt = 0: |One|
cnt = 1: |Two|
msg[3] = >> One  Two Tree  <<
cnt = 0: |One|
cnt = 1: |Two|
cnt = 2: |Tree|
msg[4] = >>There are books in the library.<<
cnt = 0: |There|
cnt = 1: |are|
cnt = 2: |books|
cnt = 3: |in|
cnt = 4: |the|
cnt = 5: |library.|

我假设文件有一个数字,用于指定参数的数量。 如果是这种情况,则创建一个循环并读取每个字符串,直到遇到定界符为止(再次,假设您被告知存在一个定界符)。 如果您没有多少个参数,则在您的结构中添加一个unsigned类型的成员,以跟踪读取的单词数。 您确实不需要创建split_function,但是如果您决定创建一个split_function,也没有什么害处:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM