如何以这种方式使用fscanf:"fscanf(fd,"%* [^ =] =%d",#). [英] how to use fscanf in this way: "fscanf(fd,"%*[^=] = %d",&num)"

查看:123
本文介绍了如何以这种方式使用fscanf:"fscanf(fd,"%* [^ =] =%d",#).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想从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:


  1. %*s:应忽略该字段(由星号定义),并且是字符串字段,由s定义.
  2. (空格):空格告诉扫描仪忽略空格,直到下一个非空白字符为止.
  3. %=:下一个字符是等号,并且可以忽略.
  4. (空格) :再次跳过空格.
  5. %f:下一个字段被视为浮点数.

  1. %*s : this field should be ignored, as defined by the asterisk, and is a string field, as defined by the s.
  2. (space) : a blank space tells the scanner to ignore whitespace until the next non-blank character.
  3. %= : the next character is an equals sign and may be ignored.
  4. (space) : skip whitespace again.
  5. %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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆