简体   繁体   English

如何正确使用fscanf?

[英]How to use fscanf properly?

I have been at this piece of code for half a day now and I still can't get it to work. 我已经在这段代码上工作了半天,但仍然无法正常工作。

while(fscanf(file, "%d {", &macroList[count]) == 1)
{
    count++;
}

for(count = 0; count < sizeof macroList/sizeof(macroList[0]); count++)
{
    printf("%d ", macroList[count]);
}

My file looks like this: 我的文件如下所示:

1{ D }2{ D }3{ D }4{ D }5{ D }6{ D }7{ D }8{ D }9{ D }10{ D }11{ D }12{ D }13{ D }14{ D }15{ D }16{ D }17{ D }18{ D }19{ D }20{ D }21{ D }22{ D }23{ D }24{ D }

My output is: 我的输出是:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

And I want it to be like this: 我希望它像这样:

1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 20 21 23 24

My questions are, why is fscanf only catching the "first entry" and how can I fix it? 我的问题是,为什么fscanf仅捕获“第一项”,我该如何解决? Also a version of this fscanf that would catch any white-space between the integer and left bracket would be appreciated. 同样,此fscanf的版本将捕获整数和左括号之间的任何空白,将不胜感激。 I have this 我有这个

while(fscanf(file, "%d %[^{]{", &macroList[count]) == 2)

but the output is the same. 但输出是相同的。

Any help is appreciated, thank you. 任何帮助表示赞赏,谢谢。

I hope the formatting isn't too confusing, first time using this. 我希望格式不要太混乱,第一次使用它。

EDIT: I forgot to mention that, although, the file exactly like that and it wasn't working for me, the real format of the file would be something like this 1{ D,DL,L }2{ _D W52 ^DL. 123 } 编辑:我忘了提一下,尽管文件完全像这样,但对我不起作用,该文件的实际格式将类似于1{ D,DL,L }2{ _D W52 ^DL. 123 } 1{ D,DL,L }2{ _D W52 ^DL. 123 } . 1{ D,DL,L }2{ _D W52 ^DL. 123 } So just about anything of any amount can be inside the brackets. 因此,括号内几乎可以包含任何数量的任何内容。

Change 更改

fscanf(file, "%d {", &macroList [count])

To

fscanf(file, "%d {%*[^}]}", &macroList [count])

Here, %d scans an int and stores it in the address of macroList[count] and then the whitespace in the format string expects any number of whitespace characters(spaces, newlines etc) including none. 在这里, %d扫描一个int并将其存储在macroList[count]的地址中,然后格式字符串中的空格期望任何数量的空格字符(空格,换行符等),包括无。 Then, the fscanf scans a { and then it scans and discards everything until a } is found( %[^}] ) and lastly, scans a } . 然后, fscanf扫描{ ,然后扫描并丢弃所有内容,直到找到}%[^}] ),最后扫描}

Your code did not work execept for the first iteration because "%d {" , expects an int first, then any number of whitespace characters including none and a opening bracket( { ). 您的代码除了第一次迭代外没有任何作用,因为"%d {"首先需要一个int ,然后是任意数量的空格字符,包括无空白字符和左括号( { )。 This is ok for the first run but not for the rest of the iterations as the next thing to be scanned is " D }" and this dosen't match the format string "%d {" . 对于第一次运行,这是可以的,但对于其余的迭代而言,这是没有问题的,因为要扫描的下一个对象是" D }"并且这与格式字符串"%d {"匹配。

Note that the above method to extract data from a file will fail(except for the first iteration) if there is nothing between { and } . 请注意,如果{}之间没有任何内容,则上述从文件中提取数据的方法将失败(第一次迭代除外)。 So, use the following as suggested by @chux in the comments below: 因此,请在以下注释中使用@chux建议的内容

fscanf(file, "%d {%*[^}]" ,&macroList [count]);
fscanf(file, "}");

When you use "%d {" , you will read a int , then a whitespace and a opening backet. 当您使用"%d {" ,您将读取一个int ,然后一个空格和一个开头backet。 Thats why your first output is correct. 这就是为什么您的第一个输出正确的原因。 After that, the same command will be used to keep reading the file, but now the string to be read is " D }" , that is, a whitespace, an uppercase 'D' and closing bracket, by no means what you actually wanted. 之后,将使用相同的命令来继续读取文件,但是现在要读取的字符串为" D }" ,即空格,大写字母“ D”和右括号,决不是您真正想要的。

Use "%d { D }" inside your fscanf to fix your issue, for that is exactly the repeating pattern: it will read an int , a whitespace, an opening bracket, another whitespace, a 'D',a final whitespace and then a closing bracket. fscanf使用"%d { D }"来解决问题,因为这恰好是重复模式:它将读取一个int ,一个空格,一个左括号,另一个空格,一个'D',最后一个空格,然后右括号。 Also, keep in mind to use the fopen and fclose functions correctly. 另外,请记住正确使用fopenfclose函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM