The language I am using is C
I am trying to scan data from a file, and the code segment is like:
char lsm;
long unsigned int address;
int objsize;
while(fscanf(mem_trace,"%c %lx,%d\n",&lsm,&address,&objsize)!=EOF){
printf("%c %lx %d\n",lsm,address,objsize);
}
The file which I read from has the first line as follows:
S 00600aa0,1
I 004005b6,5
I 004005bb,5
I 004005c0,5
S 7ff000398,8
The results that show in stdout is:
8048350 134524916
S 600aa0 1
I 4005b6 5
I 4005bb 5
I 4005c0 5
S 7ff000398,8
Obviously, the results had an extra line which comes nowhere.Is there anybody know how this could happen?
Thx!
This works for me on the data you supply:
There are multiple changes. The simplest and least consequential is the change from
fscanf()toscanf(); that’s for my convenience.One important change is the type of
lsmfrom a singlecharto an array of two characters. The format string then uses%1sreads one character (plus NUL'\0') into the string, but it also (and this is crucial) skips leading blanks.Another change is the use of
== 3instead of!= EOFin the condition. If something goes wrong,scanf()returns the number of successful matches. Suppose that it managed to read a letter but what followed was not a hex number; it would return 1 (not EOF). Further, it would return 1 on each iteration until it could find something that matched a hex number. Always test for the number of values you expect.The output format was tidied up with the
%9lx. I was testing on a 64-bit system, so the 9-digit hex converts fine. One problem withscanf()is that if you get an overflow on a conversion, the behaviour is undefined.Output:
Why did you get the results you got?
The first conversion read a space into
lsm, but then failed to convertSinto a hex number, so it was left behind for the next cycle. So, you got the left-over garbage printed in the address and object size columns. The second iteration read theSand was then in synchrony with the data until the last line. The newline at the end of the format (like any other white space in the format string) eats white space, which is why the last line worked despite the leading blank.