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

标签 c string file-io

我有一个格式为字符串的文件,后跟一长串由空格分隔的 float :

字符串(空格) float (空格)... float

字符串(空格) float (空格)... float

.

.

.

字符串(空格) float (空格)... float

每行的字符串和 float 都将放入一个结构中,目前我的做法是使用 fgets 将每行存储为字符串,然后递增该字符串,检查空格之间的子字符串,然后将这些字符串转换为 float 并将其存储在我的结构中。

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

最佳答案

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

包含数据的“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;
}

但是考虑到您所说的 float 是可变的,可能的解决方案可能是这样的:

包含数据的“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 文件每行中数据数量的变化来解析文本文件比第一种情况要花费更多的精力。

关于c - 在 C 中从较长字符串中提取子字符串的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56287470/

相关文章:

Clang 与 1 优化的倒数

相当于 python 字符串切片的 JavaScript

java - 线程中出现异常 "AWT-EventQueue-0"java.lang.NullPointerException 错误,该怎么办?

c - 兼容 C90 的编译器是否必须考虑 CPU 的指令重新排序?

c - 尝试交换用户输入数组中的多个值

c - 在不创建新文件的情况下查找并替换文本文件中的特定字符串

c - 为什么使用字符指针声明或定义字符串

r - 在R中使用xtable写入文件

c - 将文本文件读入二维数组

c - C 中函数指针语法的用途是什么?