简体   繁体   中英

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? Also a version of this fscanf that would catch any white-space between the integer and left bracket would be appreciated. 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 } . 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. Then, the fscanf scans a { and then it scans and discards everything until a } is found( %[^}] ) and lastly, scans a } .

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( { ). 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 {" .

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:

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

When you use "%d {" , you will read a int , then a whitespace and a opening 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.

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. Also, keep in mind to use the fopen and fclose functions correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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