【问题标题】:How to create an array consisting of the tokens created using the strtok function?如何创建由使用 strtok 函数创建的标记组成的数组?
【发布时间】:2015-10-04 05:08:09
【问题描述】:

我是 .ini 文件的新手,因此这个 qn(可能看起来很傻)。我创建了一个 .ini 文件并通过我的 C 程序访问它。 ini 文件如下所示:

 [key]
 title = A,H,D

C 程序使用以下方式访问它:

 LPCSTR ini ="C:\\conf.ini;
 char var[100];
 GetPrivateProfileString("key", "title", 0, var, 100, ini);

 printf("%s", var);
 char* buffer = strtok(var, ", ");
do{
    printf("%s", buffer);

    if (strcmp(buffer, "A")==0)
        printf("Hello");

    puts("");
}while ((buffer=strtok(NULL, ", "))!= NULL);

输出看起来像:

  A H D F G IAHello
  H
  D
  F
  G

现在我需要做的是再次使用这些单独的标记在我的 C 程序中形成一个带有索引的数组。例如:

  char x[A, H, D, F, G]

所以当我引用索引 2 时,x[2] 应该给我“D”。有人可以建议一种方法来做到这一点。我以前从未使用过 strtok,因此非常困惑。提前谢谢你。

【问题讨论】:

  • returnValue2 包含什么?,这段代码是否完整?
  • 可以使用指针数组来保存strtok返回的所有字符串
  • @SaurabhMeshram:很抱歉造成混乱。我编辑了我的代码

标签: c arrays ini strtok


【解决方案1】:

这个问题与其他关于获取外部信息并将其存储在数组中的问题非常相似。

问题是数组中要存储的元素数量。

您可以使用链接列表,但在本例中,我将扫描文件,获取数组所需的项目总数 - 然后再次解析文件数据 - 将项目存储在数组中。

根据您发布的示例,第一个循环遍历并计算要存储的项目。我将做第二个循环作为示例 - 请注意在我的示例中您将创建 nTotalItems 并计算项目的数量,将其存储在 nTotalItems ...我假设您要存储一个字符串,而不仅仅是一个字符...

另外请注意这是一个草稿示例,在工作中完成 - 仅显示将令牌存储到数组中的方法,因此没有错误检查 ec

 // nTotalItems has already been calculated via the first loop...
 char** strArray = malloc( nTotalItems * sizeof( char* ));
 int    nIndex  = 0;

 // re-setup buffer
 buffer = strtok(var, ", ");
 do {

     // allocate the buffer for string and copy...    
     strArray[ nIndex ] = malloc( strlen( buffer ) + 1 );
     strcpy( strArray[ nIndex ], buffer );

     printf( "Array %d = '%s'\n", nIndex, strArray[ nIndex ] );

     nIndex++;

 } while ((buffer=strtok(NULL, ", "))!= NULL);

【讨论】:

    【解决方案2】:

    只需使用an INI parser that supports arrays

    INI 文件:

    [my_section]
    title = A,H,D
    

    C 程序:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <confini.h>
    
    #define MY_ARRAY_DELIMITER ','
    
    struct configuration {
        char ** title;
        size_t title_length;
    };
    
    static char ** make_strarray (size_t * arrlen, const char * src, const size_t buffsize, IniFormat ini_format) {
    
        *arrlen = ini_array_get_length(src, MY_ARRAY_DELIMITER, ini_format);
        char ** const dest = *arrlen ? (char **) malloc(*arrlen * sizeof(char *) + buffsize) : NULL;
        if (!dest) { return NULL; }
        memcpy(dest + *arrlen, src, buffsize);
        char * iter = (char *) (dest + *arrlen);
        for (size_t idx = 0; idx < *arrlen; idx++) {
            dest[idx] = ini_array_release(&iter, MY_ARRAY_DELIMITER, ini_format);
            ini_string_parse(dest[idx], ini_format);
        }
    
        return dest;
    
    }
    
    static int ini_handler (IniDispatch * this, void * v_conf) {
    
        struct configuration * conf = (struct configuration *) v_conf;
    
        if (this->type == INI_KEY && ini_string_match_si("my_section", this->append_to, this->format)) {
    
            if (ini_string_match_si("title", this->data, this->format)) {
    
                /*  Save memory (not strictly needed)  */
                this->v_len = ini_array_collapse(this->value, MY_ARRAY_DELIMITER, this->format);
    
                /*  Allocate a new array of strings  */
                if (conf->title) { free(conf->title); }
                conf->title = make_strarray(&conf->title_length, this->value, this->v_len + 1, this->format);
                if (!conf->title) { return 1; }
    
            }
    
        }
    
        return 0;
    
    }
    
    static int conf_init (IniStatistics * statistics, void * v_conf) {
    
        *((struct configuration *) v_conf) = (struct configuration) { NULL, 0 };
        return 0;
    
    }
    
    int main () {
    
        struct configuration my_conf;
    
        /*  Parse the INI file  */
        if (load_ini_path("C:\\conf.ini", INI_DEFAULT_FORMAT, conf_init, ini_handler, &my_conf)) {
    
            fprintf(stderr, "Sorry, something went wrong :-(\n");
            return 1;
    
        }
    
        /*  Print the parsed data  */
        for (size_t idx = 0; idx < my_conf.title_length; idx++) {
    
            printf("my_conf.title[%d] = %s\n", idx, my_conf.title[idx]);
    
        }
    
        /*  Free the parsed data  */
        if (my_conf.title_length) {
    
            free(my_conf.title);
    
        }
    
        return 0;
    
    }
    

    输出:

    my_conf.title[0] = A
    my_conf.title[1] = H
    my_conf.title[2] = D
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-17
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多