[英]C fscanf input format
I have an input file that contains lines of the following format: 我有一个包含以下格式的行的输入文件:
%s %d %d %d %lf %lf ... %lf\n
where the number of double
values is unknown, but for my calculations I accept only first 15 of them. 其中
double
值的数量未知,但是对于我的计算,我只接受其中的前15个。
The problem I can't figure out is, when I get to the line like this: 我不知道的问题是,当我到达这样的行时:
City0 28 2 2016 1 2 3 4 5 6 7 8 9 10
City1 28 2 2016 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
City2 1 3 2016 1 2 3 4 5
and I correctly assign respective values to a certain structure, I get the following: 并且我将各自的值正确分配给某个结构,得到以下结果:
City0 28 2 2016 Number of measures: 10
City1 28 2 2016 Number of measures: 15
16 17 18 19 Number of measures: 1
City2 1 3 2016 Number of measures: 5
How do I read(to nowhere)/ignore everything until I get to the end of the line, and then start reading the next line as usual? 如何读(无处)/忽略所有内容,直到到达行尾,然后照常开始阅读下一行? I need the following output:
我需要以下输出:
City0 28 2 2016 Number of measures: 10
City1 28 2 2016 Number of measures: 15
City2 1 3 2016 Number of measures: 5
I tried this but ain't got any more ideas: 我尝试了这个,但没有更多的主意:
i=0; char character;
while (fscanf(fp, "%s %d %d %d", c.name, &c[i].date.day,
&c[i].date.month, &c[i].date.year)==4 && i<number_of_cities) {
while (fscanf(fp, "%lf", &c[i].measures[j])==1 && j<15) {
j++;
}
if (j==15) {
while (fscanf(fp, "%s", character)!='\n') {}
}
c[i].mnum = j;
j=0;
i++;
}
You can use fscanf()
to read in an entire line of input, use sscanf()
to scan the first four values, and use sscanf()
again in a loop to read the double
values. 您可以使用
fscanf()
读取整行输入,使用sscanf()
扫描前四个值,然后在循环中再次使用sscanf()
读取double
值。 The trick here is to use the %n
directive to save the position of the next read location in the string. 这里的技巧是使用
%n
指令将下一个读取位置的位置保存在字符串中。
Here is an example. 这是一个例子。 Note that
size_t
is used for array indices, as this is an unsigned integer type that is guaranteed to hold any array index. 请注意,
size_t
用于数组索引,因为这是一个无符号整数类型,可以保证保存任何数组索引。 Also note that there is some error-checking when opening the file, and when scanning the beginning of a line. 另请注意,在打开文件和扫描行的开头时会进行一些错误检查。 If the initial elements of the line do not match expected values, the program exits with an error message.
如果该行的初始元素与期望值不匹配,则程序退出并显示错误消息。 This error-checking could be tightened up a bit;
这种错误检查可能会更严格一些。 for example, if the year is entered as a floating point value, such as
2016.0
, the input will be accepted, but the values stored in measures[]
will begin with the 0
following the decimal point. 例如,如果将年份作为浮点值输入(例如
2016.0
,则将接受输入,但存储在2016.0
measures[]
的值将从小数点后的0
开始。
#include <stdio.h>
#include <stdlib.h>
struct Data {
char name[1000];
struct {
int day;
int month;
int year;
} date;
size_t mnum;
double measures[15];
};
int main(void)
{
size_t i = 0, j = 0;
char buffer[1000];
char *read_ptr = buffer;
int n_read = 0;
size_t number_of_cities = 3;
struct Data c[number_of_cities];
FILE *fp;
if ((fp = fopen("datafile.txt", "r")) == NULL) {
fprintf(stderr, "Unable to open file\n");
exit(EXIT_FAILURE);
}
while (fgets(buffer, 1000, fp) != NULL) {
if (sscanf(buffer, "%s %d %d %d %n", c[i].name, &c[i].date.day,
&c[i].date.month, &c[i].date.year, &n_read) != 4) {
fprintf(stderr, "Incorrect input format\n");
exit(EXIT_FAILURE);
}
read_ptr += n_read;
while (sscanf(read_ptr, "%lf %n", &c[i].measures[j], &n_read) == 1 &&
j < 15) {
read_ptr += n_read;
++j;
}
c[i].mnum = j;
++i;
j = 0;
read_ptr = buffer;
if (i == number_of_cities) {
break;
}
}
for (i = 0; i < number_of_cities; i++) {
printf("%s %d %d %d Number of measures: %zu\n",
c[i].name,
c[i].date.day, c[i].date.month, c[i].date.year,
c[i].mnum);
for (j = 0; j < c[i].mnum; j++) {
printf("%5g", c[i].measures[j]);
}
putchar('\n');
}
return 0;
}
Program output using your example data as input: 使用示例数据作为输入的程序输出:
City0 28 2 2016 Number of measures: 10
1 2 3 4 5 6 7 8 9 10
City1 28 2 2016 Number of measures: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
City2 1 3 2016 Number of measures: 5
1 2 3 4 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.