首页 > 解决方案 > 如何在文件 *stream 中的特定点停止并扫描某些值?

问题描述

我有一个名为 test.txt 的文件,该文件包含:

<this is a test = 1>

<more tests = 42 and more "34">

<start>10.213123 41.21231 23.15323</start>

<random stuff = "4">

<blah 234>

当我看到<start>我想将 3 个数字扫描成这样的双倍后:

x = 10.213123
y = 41.21231
z = 23.15323

我有点困惑,因为在这里, fgets 扫描整行,我怎样才能将 3 个数字扫描成双精度数?因为数字可以有不同的长度?我这样做是为了打印出它从文件中读取的内容,但我无法理解它。

void  print_lines(FILE *stream) {
    char line[MAX_LINE_LENGTH];
    while (fgets(line, MAX_LINE_LENGTH, stream) != NULL) {
        fputs(line, stdout);
    }
}

标签: cfilewhile-loopdoublefilestream

解决方案


就在您看到<start>然后将 3 个数字扫描成双倍。您在变量中有行内容line,您可以使用strtod将字符串扫描成双精度。您甚至可以使用sscanf(line, "<start>%lf %lf %lf</start>", &x, &y, &z);,但使用 strtod 更好地处理错误。

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>

#define MAX_LINE_LENGTH 1024

void  print_lines(FILE *stream) {
    double a, b, c;
    char line[MAX_LINE_LENGTH];
    while (fgets(line, MAX_LINE_LENGTH, stream) != NULL) {
        char *pnt;
        // locate substring `<start>` in the line
        if ((pnt = strstr(line, "<start>") != NULL)) {
            // advance pnt to point to the characters after the `<start>`
            pnt = &pnt[sizeof("<start>") - 1];
            char *pntend;

            // scan first number
            a = strtod(pnt, &pntend);
            if (pnt == pntend) {
                fprintf(stderr, "Error converting a value.\n");
                // well, handle error some better way than ignoring.
            }
            pnt = pntend;
            // scan second number
            b = strtod(pnt, &pntend);
            if (pnt == pntend) {
                fprintf(stderr, "Error converting a value.\n");
                // well, handle error some better way than ignoring.
            }
            pnt = pntend;
            // scan third number
            c = strtod(pnt, &pntend);
            if (pnt == pntend) {
                fprintf(stderr, "Error converting a value.\n");
                // well, handle error some better way than ignoring.
            }

            printf("Read values are %lf %lf %lf\n", a, b, c);
        } else {
            // normal line
            //fputs(line, stdout);
        }
    }
}

int main()
{
    print_lines(stdin);
    return 0;
}

推荐阅读