从 C 中的较长字符串中提取子字符串的最佳方法是什么

What is the best way to extract substrings from a longer string in C

我有一个格式为字符串的文件,后跟一长串由 spaces:

分隔的浮点数

字符串 (space) 浮点数 (space) ... 浮点数

字符串 (space) 浮点数 (space) ... 浮点数

.

.

.

字符串 (space) 浮点数 (space) ... 浮点数

每一行的字符串和浮点数都将放在一个结构中,目前我这样做的方式是使用 fgets 将每一行存储为一个字符串,然后递增该字符串,检查 [= 之间的子字符串28=]s,然后将这些字符串转换为浮点数并将它们存储在我的结构中。

这变得非常乏味和复杂。有更好的方法吗?

根据要固定或可变的变量数量,有两种可能的方法。如果浮动数量固定,可能的解决方案可能是:

'test_data.txt' 包含数据的文件:

test1 1.41 1.73 2.78 3.14
test2 2.41 2.73 3.78 4.14

读取数据的源文件可能是:

#include <stdio.h>

int main(int argc, char ** argv)
{
        FILE * file = fopen("test_data.txt", "r");
        if (file == NULL)
        {
                printf("Cannot open file.\n");
                return 1;
        }

        char string[32] = { 0 };
        float f1, f2, f3, f4;
        while (feof(file) == 0)
        {
                fscanf(file, "%s %f %f %f %f", string, & f1, & f2, & f3, & f4);
                printf("For string: %s values are:\n\t%f %f %f %f\n", string, f1, f2, f3, f4);
        }

        fclose(file);
        return 0;
}

但是考虑到你所说的浮点数是可变的,可能的解决方案可能是这样的:

'test_data.txt' 包含数据的文件:

test1 1.41 1.73 2.78 3.14
test2 2.41 2.73 3.78 4.14 5.15

读取数据的源文件可能是:

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

static void remove_trailing_spaces(char ** begin)
{
        while (isspace(** begin))
                ++(* begin);
        return;
}

static void get_string(char ** begin, char * output)
{
        remove_trailing_spaces(begin);

        char * end = * begin;
        while (isalnum(* end))
                ++end;

        strncpy(output, * begin, (int)(end - * begin));
        * begin = end;
        return;
}

static void get_float(char ** begin, float * output)
{
        remove_trailing_spaces(begin);

        char * end;
        * output = strtof(* begin, & end);
        * begin = end;
        return;
}

int main(int argc, char ** argv)
{
        FILE * file = fopen("test_data.txt", "r");
        if (file == NULL)
        {
                printf("Cannot open file\n");
                return 1;
        }

        char buffer[1024] = { 0 };
        char string[32] = { 0 };
        while (feof(file) == 0)
        {
                if (fgets(buffer, 1024, file) != NULL)
                {
                        char * begin = & buffer[0];
                        get_string(& begin, string);
                        printf("For string: %s values are:\n\t", string);

                        while ((feof(file) == 0) && (* begin != '\n'))
                        {
                                float f = 0.0;
                                get_float(& begin, & f);
                                printf("%f ", f);
                        }
                        printf("\n");
                }
        }
        fclose(file);
        return 0;
}

请记住,这可能不是最佳解决方案。它仅表明在 test_data.txt 文件的每一行中更改数据数量的文本文件的解析比第一种情况更费力。