c - 如何在文件 *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);
}
}
解决方案
就在您看到<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;
}
推荐阅读
- wpf - WPF DataGrid 文本搜索在单元格对齐更改后不起作用
- c# - 如何验证父子树的正确层次结构
- python - HTTPserver python Hello world 系统找不到指定的路径
- c# - 编译错误:“Program.Node”不包含采用 3 个参数的构造函数
- python - 无法在 Beautiful Soup 中从 eBay 获取数据
- mysql - GROUP BY id_recruiter ORDER BY ID DESC 不起作用
- mdriven - MDriven 兼容.net 标准库(.net 核心)
- apache-spark - 如何在火花中组合两列数据集
- c++ - 对于 C++ 中的每个循环
- php - 将多维数组转换为简单数组