如何以这种方式使用fscanf:"fscanf(fd,"%* [^ =] =%d",#). [英] how to use fscanf in this way: "fscanf(fd,"%*[^=] = %d",&num)"
问题描述
我想从txt文件中读取一些数据.例如:
这是"1.txt"的内容:
这是一些数据:
a = 1.25;
b = 5.34;
...
这是其他数据:
c [0,0] = 7.684;
c [0,1] = 6.357;
c [0,2] = 1.325;
...
我想将c [i,j]的值读取为array [k],
I want to read some of the data from a txt file. For example:
This is content of "1.txt":
this is some data:
a = 1.25;
b = 5.34;
...
this is the other data:
c[0,0] = 7.684;
c[0,1] = 6.357;
c[0,2] = 1.325;
...
I want to read value of c[i,j] to array[k],
FILE *file;
file= fopen("1.txt","r");
while ( (sn = fscanf(file,"%*[^=] = %f",&znk) ) != EOF )
{
if ( sn > 0 )
array[i++] = znk;
}
fclose(file);
但是我无法以这种方式得到想要的东西,因为我也将"a","b"的值读入了数组[k],应该如何更改
But I can not get what I want in this way,because I also read the value of "a","b" into array[k], how should I change
fscanf(file,"%*[^=] = %f",&znk)
非常感谢您!
Thank you very much !
推荐答案
尝试:
fscanf(file,"%*s %= %f",&znk);
[edit]
综上所述,我们可以将模式分解为以下部分:
[edit]
Looking at the above we can break the pattern down into its parts as follows:
-
%*s
:应忽略该字段(由星号定义),并且是字符串字段,由s
定义. -
-
%=
:下一个字符是等号,并且可以忽略. -
-
%f
:下一个字段被视为浮点数.
%*s
: this field should be ignored, as defined by the asterisk, and is a string field, as defined by thes
.%=
: the next character is an equals sign and may be ignored.%f
: the next field is treated as a floating point number.
格式类型的完整详细信息可以在MSDN上的此处找到 [
Full details of the format types can be found here on MSDN[^].
[/edit]
这篇关于如何以这种方式使用fscanf:"fscanf(fd,"%* [^ =] =%d",#).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!