c 从文件中读入数组并拆分

标签 c malloc fread

我有一个特定的 txt 文件(例如 - dic.txt),其中单词按以下顺序出现:

hello - ola - hiya \n
chips - fries - frenchfries \n

我需要将文件的内容读入一个字符串数组: 例如:

array[0]  : [hello,ola,hiya]
array[1]  : [chips,fries,frenchfries]

我正在考虑使用 strtok 将文件中的每一行拆分为一个字符串(在将整个文件复制到一个字符串并计算行数之后),但我想不通如何将每一行 ("hello - ola - hiya\n") 拆分为单词,并将每个数组存储到数组中(数组中的字符串数组)。

我正在考虑使用 malloc 为每行单词分配内存,并将指向字符串数组的指针存储到数组中,但我很乐意收到任何建议。

最佳答案

从文件中读取行然后将它们拆分为标记的直接方法是使用 fgets 读取行,然后使用 strtok 将每一行拆分为标记:

int main(int argc, char *argv[])
{
    // Check for arguments and file pointer omitted
    FILE *f = fopen(argv[1], "r");

    for (;;) {
        char line[80];
        char *token;

        if (fgets(line, 80, f) == NULL) break;
        token = strtok(line, " -\n");
        while (token) {
            // Do something with token, for example:
            printf("'%s' ", token);
            token = strtok(NULL, " -\n");
        }
    }

    fclose(f);
    return 0;
}

只要您文件中的所有行都短于 80 个字符,这种方法就很好。它适用于每行可变数量的标记。

您提到了处理线条内存的问题。上面的例子假设内存处理是由每个单词的数据结构完成的。 (它不是示例的一部分,它只是打印标记。)

您可以为每一行malloc 内存,这比每行严格的字符限制更灵活,但您最终会分配大量内存。好处是您的单词不需要额外的内存,它们可以只是指向行的指针,但您必须注意为行正确分配内存 - 然后释放它。

如果您将整个文本文件读取到一个连续的内存块中,那么您基本上就完成了内存存储,只要您保持该 block “活着”,只要您的单词还在:

char *slurp(const char *filename, int *psize)
{
    char *buffer;
    int size;
    FILE *f;

    f = fopen(filename, "r");
    if (f == NULL) return NULL;

    fseek(f, 0, SEEK_END);
    size = ftell(f);
    fseek(f, 0, SEEK_SET);

    buffer = malloc(size + 1);
    if (buffer) {
        if (fread(buffer, 1, size, f) < size) {
            free(buffer);
        } else {
            buffer[size] = '\0';
            if (psize) *psize = size;
        }
    }

    fclose(f);
    return buffer;
}

有了那 block 内存,你可以先通过寻找下一个换行符来寻找行,然后像上面那样使用strtok:

int main(int argc, char *argv[])
{
    char *buffer;    // contiguous memory chunk
    char *next;      // pointer to next line or NULL for last line

    buffer = slurp(argv[1], NULL);    
    if (buffer == NULL) return 0;

    next = buffer;
    while (next) {
        char *token;
        char *p = next;

        // Find beginning of the next line, 
        // i.e. the char after the next newline
        next = strchr(p, '\n');
        if (next) {
            *next = '\0';      // Null-terminate line
            next = next + 1;   // Advance past newline
        }

        token = strtok(p, " -\n");
        while (token) {
            // Do something with token, for example:
            printf("'%s' ", token);
            token = strtok(NULL, " -\n");
        }        
    }

    free(buffer);             // ... and invalidate your words        
    return 0;
}

如果你使用fscan,你总是将找到的标记复制到一个临时缓冲区,当你将它们存储在你的字典结构中时,你必须再次使用strcpy复制它们>。这是很多复制。在这里,您读取并分配一次,然后使用指向 block 的指针。 strtok 以 null 终止标记,因此您的 block 是 C 字符串链。

将整个文件读入内存通常不是一个好的解决方案,但在这种情况下,文件基本上就是数据,这是有意义的。

(注意:所有这些关于内存的讨论都不会影响字典结构、树中的节点和线性列表或其他任何内容所需的内存。它只是关于正确存储字符串。)

关于c 从文件中读入数组并拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20159834/

相关文章:

c++ - 如何获取文件名命令行参数的完整路径?

c - 将指针附加到 pthread

c - 尝试删除 malloc 的使用 - 这里有替代方案吗?

c - 当我必须指定内存大小时,malloc 数组和常规数组之间有什么区别?

algorithm - 将RGB图像读入二进制并在Matlab中显示为RGB

php - 读取() : supplied argument is not a valid stream resource

c - 使用 peg/leg 时缓冲区溢出

c++ - XGboost 上的 mingw' 中未启用外部存储器

c - malloc 的 K&R C 示例中似乎不需要的行?

c - 在 c : when do I have to use malloc 中实现字符串队列