c - 根据分隔符分割一串整数并转换为int类型?

标签 c arrays token tokenize

我正在编写一个程序,该程序接受一个有序数字对文件作为输入,我想拆分这些有序对并将它们转换为整数以存储在数组中。

文件可能是这样的:

0 1
1 4
9 11
12 45

我想编写一个函数,它接受该行(假设在程序的另一部分已经以 null 终止),在空格处分割数字,然后将它们存储在大小为 2 的整数数组中:

 int *store = malloc(2 * sizeof(store));

我已经研究过 strtok 但我是否正确地说它不是线程安全的,因此不是这种情况下的最佳选择?此外,它不会将数字转换为整数,而这是该函数也需要具备的功能。

如何解决这个问题?

最佳答案

如果您想将未知数量的有序对读取到有序对数组中,有多种方法可以实现此目的。正如评论中所述,如果您可以保证每行始终有 2 个整数,那么读取/将值转换为整数的最简单方法可能是使用 fscanf

(当每行上的数据格式完全相同时,这是我推荐 fscanf 而不是 fgets唯一情况之一,并且sscanf,然后只是为了简化示例)

当要读取的元素数量未知时,基本方案是为一些合理预期的数字对分配内存,然后根据需要realloc。由于您正在读取有序对,因此可以通过为指向两个整数的数组的指针分配存储来简化该过程。例如,您的 ordered-pairs op 数组声明如下:

    int (*op)[2] = NULL;

您可以为 MXLINE 个对分配存储空间:

    op = calloc (MXLINE, sizeof *op);

(您可以使用malloccalloc来分配)

分配初始内存来保存有序对后,您可以简单地使用包含两个十进制转换说明符(例如 "%d %d")的格式字符串读取每一行,并获得 fscanf 执行转换。您必须验证发生的 2 转换,然后将值存储在数组中。

记录您添加的对的数量,以便在达到限制时可以重新分配。一个例子如下所示。不要忘记在不再需要时释放所有内存,并通过内存检查器运行代码以验证动态分配内存的使用情况。

使用数据文件将其全部放在一个简短的示例中,您可以执行如下操作。注意:程序将从命令行上作为第一个参数给出的文件读取(如果没有给出文件名,则默认从 stdin 读取):

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

#define MXLINE 2

int main (int argc, char **argv) {

    int i, idx = 0, mxline = MXLINE;
    int (*op)[2] = NULL;
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

    /* allocate/validate initial memory */
    if (!(op = calloc (MXLINE, sizeof *op))) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        return 1;
    }

    /* read each ordered pair into 'op' array */
    while (fscanf (fp, "%d %d", &op[idx][0], &op[idx][1]) == 2) {
        if (++idx == mxline) { /* realloc when limit reached */
            void *tmp = realloc (op, sizeof *op * mxline * 2);
            if (!tmp) { /* validate reallocation */
                fprintf (stderr, "error: realloc failed.\n");
                break;
            }
            op = tmp;    /* assign reallocated pointer to op */
            mxline *= 2; /* update line allocation limit */
        }
    }

    for (i = 0; i < idx; i++) /* show ordered pairs */
        printf ("pair[%3d] %3d : %3d\n", i, op[i][0], op[i][1]);

    free (op);  /* free allocated memory */

    if (fp != stdin)    /* close file if not stdin */
        fclose (fp);

    return 0;
}

示例使用/输出

$ ./bin/array_ptr2array <dat/orderedpairs.txt
pair[  0]   0 :   1
pair[  1]   1 :   4
pair[  2]   9 :  11
pair[  3]  12 :  45

内存/错误检查

不要忘记验证您对动态分配内存的使用,以及您在不再需要时释放所有内存:

$ valgrind ./bin/array_ptr2array <dat/orderedpairs.txt
==8107== Memcheck, a memory error detector
==8107== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8107== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==8107== Command: ./bin/array_ptr2array
==8107==
pair[  0]   0 :   1
pair[  1]   1 :   4
pair[  2]   9 :  11
pair[  3]  12 :  45
==8107==
==8107== HEAP SUMMARY:
==8107==     in use at exit: 0 bytes in 0 blocks
==8107==   total heap usage: 3 allocs, 3 frees, 112 bytes allocated
==8107==
==8107== All heap blocks were freed -- no leaks are possible
==8107==
==8107== For counts of detected and suppressed errors, rerun with: -v
==8107== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)

(注意:初始分配是针对 MXLINE (2) 对。这是故意这样做的,以强制重新分配。在现实世界中您需要尽量减少必须分配内存的次数。因此,如果您认为可以有 20 个左右对,请将初始分配设置为 2432 对,或者无论您对您将拥有的数字的最佳合理猜测是什么(再加上几个以便更好地衡量))

仔细检查一下,如果有任何问题请告诉我。

关于c - 根据分隔符分割一串整数并转换为int类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299842/

相关文章:

c++ - Mat 和 setMouseCallback 函数

c - 系统函数和堆栈

c - while循环导致程序出现许多问题

Javascript 从对象表调用函数

solr - 为什么 solr RemoveDuplicatesTokenFilterFactory 不起作用?

string - 什么是多 token 字符串?

c - Linux 中的每个线程 CPU 统计信息

c++ - 如何将文件中的值输入到矩阵中?

arrays - 将字节数组转换为字符串

ruby-on-rails - 如何使用 SecureRandom.urlsafe_base64?