如何将getline()的每个字符串存储在(动态)字符串数组中?

我正在使用该getline()函数获取stdin. 每一行都是一个不同长度的字符串:

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

int main() {
    char *line = NULL;
    size_t foo = 0;
    ssize_t reader;

    while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
        printf("%s", line);
    }

    free(line);
    return 0;
}

在每次迭代中,line是一个字符串并且包含当前行。如何获取每个字符串并将其存储在数组中?我尝试了几件事情,但它们都没有奏效,或者它们只会导致内存访问失败:(


我希望我的问题很清楚?如果不是,请告诉我,我会更改它!

回答

除非您预先知道需要多少行,否则您将不得不动态分配数组,例如:

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

int main() {
    char *line = NULL;
    size_t foo = 0;
    ssize_t reader;
    int result = 0;

    int numlines = 0, maxlines = 10;
    char **lines = malloc(sizeof(char*) * maxlines);

    if (!lines) {
        printf("error allocating arrayn");
    }
    else {
        while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
            printf("%s", line);

            if (numlines == maxlines) {
                maxlines *= 2; // <-- or use whatever threshold makes sense for you
                char **newlines = realloc(lines, sizeof(char*) * maxlines);
                if (!newlines) {
                    printf("error reallocating arrayn");
                    result = -1;
                    break;
                }
                lines = newlines;
            }

            lines[numlines] = line;
            ++numlines;

            line = NULL;
            foo = 0;
        }
        free(line); // <-- in case getline() or realloc() failed...

        // use lines up to numlines as needed

        // free lines
        for(int i = 0; i < numlines; ++i) {
            free(lines[i]);
        }
        free(lines);
    }

    return result;
}


回答

您需要创建一个在需要时调整大小的指针数组:

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

int main()
{
    // start with an array that ends with a NULL pointer
    // (like argv does)
    size_t numLines = 0;
    char **lines = malloc( ( numLines  + 1 ) * sizeof( *lines ) );

    lines[ numLines ] = NULL;

    // break the loop explicitly - easier to handle and much less
    // bug-prone than putting the assignment into a while statement
    for ( ;; )
    {

        // get the next line
        size_t bytes = 0UL;
        char *line = NULL;
        ssize_t result = getline( &line, &bytes, stdin );
        if ( result < 0 )
        {
            break;
        }

        // enlarge the array by one
        numLines++;
        char **tmp = realloc( lines, ( numLines + 1 ) * sizeof( *tmp ) );
        if ( !tmp )
        {
            break;
        }

        lines = tmp;

        // add the new line to the end of the array
        lines[ numLines ] = line;
        lines[ numLines + 1 ] = NULL;
    }

    // use lines - then free them 
    return( 0 );
}

这可以通过分realloc()块调用来优化,例如每 32 或 64 行。但考虑到您已经有效地malloc()每行调用一次,这可能没有多大帮助。


以上是如何将getline()的每个字符串存储在(动态)字符串数组中?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>