【问题标题】:What is the best way to extract substrings from a longer string in C从C中的较长字符串中提取子字符串的最佳方法是什么
【发布时间】:2019-05-24 07:07:54
【问题描述】:

我有一个格式为字符串的文件,后跟一长串用空格分隔的浮点数:

字符串(空格)浮点(空格)...浮点

字符串(空格)浮点(空格)...浮点

.

.

.

字符串(空格)浮点(空格)...浮点

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

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

【问题讨论】:

  • 每行的浮点数是固定的还是可变的?
  • 您目前的方法没有任何问题,而且一点也不复杂。显示您的代码。
  • 数字是变量

标签: c string file-io


【解决方案1】:

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

带有数据的'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 文件的每一行中解析文本文件的数据数量比第一种情况要多一些。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 2012-02-29
相关资源
最近更新 更多