this is the code:
printf(" DEBUG:%s\n" ,array[7] );
printf("address of %s is %p (again %d)\n",
array[7],
array[7],
strcmp("N\\A", array[7]) );
printf("5DEBUG collection:%s\n" ,array[7] );
this is the output:
DEBUG:N\A
is 0x7c0600 (again -13)
5DEBUG collection:N\A
as you can see , in the second printf - array[7] (need to point to "N\A" ) disappeared.
I have no idea whats going on here...
You are reading a Windows-formatted file on unix. Windows and unix use different line terminators. Unix uses 0x0A, whereas Windows use 0x0D followed by 0x0A.
If you have a line in the file that ends in 0x0D 0x0A, unix will treat the 0x00A as the line terminator but include the 0x0D as part of the string.
You can see this in your strcmp
, which returns -13
. Notice that it does not return zero, which means that the strings are not equal. In fact, the difference is 13, the decimal value of 0x0D, which confirms that array[7]
has a 0x0D at the end.
The other evidence of this is the odd printing behavior you're seeing. On unix, printing 0x0D causes the cursor to return to column 0 of the same line. Therefore, the second print instruction begins by printing
address of N\A
and then it encounters the 0x0D, which moves the cursor back to column 0. The remainder of the string therefore overprints the output, resulting in
is 0x7c0600 (again -13)
If you tried to debug the program in a debugger with a breakpoint on the code, you would have noticed that array[7]
has a 0x0D
at the end.
Added
This was not psychic debugging. It was actually quite straightforward. Here's the step-by-step:
array[7]
. If you had done that, you would have seen the trailing 0x0Dand the problem would have been solved in 5 seconds.strcmp
was not zero. This means that the string in array[7]
is not equal to "N\\A"
, which is your next huge clue that you should use the debugger to look at the string in array[7]
to see what it actually is.array[7]
and "N\\A"
must be in something that is not readily visible, since the first line printed okay. The options here are control characters or whitespace.strcmp
reported a difference of 13
suggests that the string in array[7]
has an 0x0D at the end: "N\\A"
ends with a \0
(numerical value zero), and a difference of 13 suggests that array[7]
ends with 0x0D, since 0x0d hex = 13 decimal.So there were four independent ways of coming to the same diagnosis. (Five if you count "Look at the string in the debugger.") Since they all agreed, this made for a rather confident conclusion. My actual analysis started with step 5, then used the other steps to confirm the diagnosis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With