[英]read a double number from file - c
I have file filled with rows of data: each row contains:我有充满数据行的文件:每行包含:
'char' space 'double'. 'char' 空格 'double'。 eg:
例如:
a 17.322\n
c 9.45\n
I want to 'eat' the \\n and the char, and read only the double number.我想“吃”\\n 和字符,并且只读取双数。 what is the best way to do it.
最好的方法是什么。
thanks, and sorry if I have English mistakes谢谢,如果我有英语错误,抱歉
I think simple fscanf
can do the job我认为简单的
fscanf
可以完成这项工作
#include <stdio.h>
int main()
{
FILE* f = fopen("test", "r");
float n;
while(fscanf(f, "%*[a-z]%f ", &n) == 1)
printf("%f, ", n);
return 0;
}
"%*"
ignores everything it reads "%*"
忽略它读取的所有内容"[az]"
reads characters from range 'a' to 'z' "[az]"
读取从 'a' 到 'z' 范围内的字符"%f"
reads a single precision floating point number (in other words float
) "%f"
读取单精度浮点数(换句话说float
)" "
ignores all whitespace characters " "
忽略所有空白字符EOF
in case of reading error (ex. end of file) or number of read values which in our case is 1 EOF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.