将字符串拆分为完全动态分配的字符串数组

Spliting a string into an array of strings completly dynamicly allocated

这个问题与 非常接近,但我更喜欢此解决方案提供的易理解性和指针说明。

所以我有一个数据文件,我从中得到了一个很长的字符数组。我想将这个字符串拆分成一个数组,在每种情况下,一个字符串对应于这个文件的一行。
我看到了解决方案,但它们都使用有限的数组,因为我不知道每行的长度,我真的需要动态分配所有这些但我找不到行的长度,因为 strtokdoesn' t 在每个字符串的末尾放置一个空字符 [=13=]

我现在得到的是这两个解决方案,但都不起作用:

int get_lines(char *file, char **lines) {
    int nb_lines = 0;
    char *token = strtok(file, "\n");
    for(int i = 0; token != NULL; i++) {
        token = strtok(NULL, "\n");
        nb_lines = i;
    }
    nb_lines++;

    lines = malloc((nb_lines + 1) * sizeof(char*));
    lines[nb_lines] = '[=10=]';

    token = strtok(file, "\n");
    for(int i = 0; token != NULL; i++) {
        token = strtok(NULL, "\n");
        int nb_char = 0;
        for(int j = 0; token[j] != '\n'; j++) //This will cause SIGSEGV because strtok don't keep the '\n' at the end
            nb_char = j;
        nb_char++;
        token[nb_char] = '[=10=]'; //This cause SIGSEGV because token's allocation finish at [nb_char-1]
        lines[i] = malloc(strlen(token) * sizeof(char)); //strlen cause SIGSEGV because I cannot place the '[=10=]' at the end of token
        printf("%s", token); //SIGSEGV because printf don't find the '[=10=]'
        lines[i] = token;
    }

    for(int i = 0; i < nb_lines; i++) {
        printf("%s", lines[i]); //SIGSEGV
    }

    return nb_lines;
}

所以你可以在上面看到我想做什么以及为什么它不起作用的想法。

下面你会看到我做的另一个尝试,但我停留在同一点:

int count_subtrings(char* string, char* separator) {
    int nb_lines = 0;
    char *token = strtok(string, separator);
    for(int i = 0; token != NULL; i++) {
        token = strtok(NULL, separator);
        nb_lines = i;
    }
    return nb_lines + 1;
}

char** split_string(char* string, char* separator) {
    char **sub_strings = malloc((count_subtrings(string, separator) + 1) * sizeof(char*));
    for(int i = 0; string[i] != EOF; i++) {
        //How to get the string[i] lenght to malloc them ?
    }
}

我的文件很大,行也可能太多,所以我不想 malloc 另一个大小为 (strlen(file) + 1) * sizeof(char) 的 table 以确保每一行都不会 SIGSEGV 和我也觉得这个解决方案很脏,如果你们有其他想法,我会很高兴。

(抱歉英文错误,我不是很好)

您使用 strtok 的方法有两个缺点:首先,strtok 修改了字符串,因此您只能传递一次原始字符串。其次,它会跳过空行,因为它会将 nelines 的延伸作为单个标记分隔符..(我不知道你是否关心这个问题。)

您可以通过一次遍历字符串来计算换行符。为您的线阵列分配内存并进行第二次传递,您在换行符处拆分字符串:

char **splitlines(char *msg)
{
    char **line;
    char *prev = msg;
    char *p = msg;

    size_t count = 0;
    size_t n;

    while (*p) {
        if (*p== '\n') count++;
        p++;
    }

    line = malloc((count + 2) * sizeof(*line));
    if (line == NULL) return NULL;

    p = msg;
    n = 0;
    while (*p) {
        if (*p == '\n') {
            line[n++] = prev;
            *p = '[=10=]';
            prev = p + 1;
        }

        p++;
    }

    if (*prev) line[n++] = prev;
    line[n++] = NULL;

    return line;
}

我分配了两个比换行数多的行指针:一个用于最后一行不以换行结尾的情况,另一个用于在末尾放置一个 NULL 标记,所以你知道你的数组在哪里结束。 (当然,您可以通过指向 size_t 的指针 return 实际行数。)

以下建议代码:

  1. 干净地编译
  2. (在堆大小的限制内)不关心输入文件大小
  3. echo 是生成的文件行数组,双倍行距,只是为了表明它有效。对于单个间距,将 puts() 替换为 printf()

现在是代码

#include <stdio.h>   // getline(), perror(), fopen(), fclose()
#include <stdlib.h>  // exit(), EXIT_FAILURE, realloc(), free()


int main( void )
{
    FILE *fp = fopen( "untitled1.c", "r" );
    if( !fp )
    {
        perror( "fopen for reading untitled1.c failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    char **lines = NULL;
    size_t availableLines = 0;
    size_t usedLines = 0;

    char *line = NULL;
    size_t lineLen = 0;
    while( -1 != getline( &line, &lineLen, fp ) )
    {
        if( usedLines >= availableLines )
        {
            availableLines = (availableLines)? availableLines*2 : 1;
            char **temp = realloc( lines, sizeof( char* ) * availableLines );
            if( !temp )
            {
                perror( "realloc failed" );
                free( lines );
                fclose( fp );
                exit( EXIT_FAILURE );
            }

            // implied else realloc successful

            lines = temp;
        }

        lines[ usedLines ] = line;
        usedLines++;
        line = NULL;
        lineLen = 0;
    }

    fclose( fp );

    for( size_t i = 0; i<usedLines; i++ )
    {
        puts( lines[i] );
    }

    free( lines );
}

鉴于以上代码位于名为:untitled1.c 的文件中,以下是输出。

#include <stdio.h>   // getline(), perror(), fopen(), fclose()

#include <stdlib.h>  // exit(), EXIT_FAILURE, realloc(), free()





int main( void )

{

    FILE *fp = fopen( "untitled1.c", "r" );

    if( !fp )

    {

        perror( "fopen for reading untitled1.c failed" );

        exit( EXIT_FAILURE );

    }



    // implied else, fopen successful



    char **lines = NULL;

    size_t availableLines = 0;

    size_t usedLines = 0;



    char *line = NULL;

    size_t lineLen = 0;

    while( -1 != getline( &line, &lineLen, fp ) )

    {

        if( usedLines >= availableLines )

        {

            availableLines = (availableLines)? availableLines*2 : 1;

            char **temp = realloc( lines, sizeof( char* ) * availableLines );

            if( !temp )

            {

                perror( "realloc failed" );

                free( lines );

                fclose( fp );

                exit( EXIT_FAILURE );

            }



            // implied else realloc successful



            lines = temp;

        }



        lines[ usedLines ] = line;

        usedLines++;

        line = NULL;

        lineLen = 0;

    }



    fclose( fp );



    for( size_t i = 0; i<usedLines; i++ )

    {

        puts( lines[i] );

    }



    free( lines );

}