0
char hexe;
int hex1;
FILE *pFile;
pFile = fopen("address01", "r");

while (fscanf(pFile,"%c %d",&hexe, &hex1) != EOF) { //ERROR likely here

printf("%c %d", hexe, hex1);                        //ERROR likely here 

}

My address01 file:

10
20
22
18
E10
210
12

It is printing 18 and 12 twice. Why those numbers and how do I get it to act normal?

(Additionally, I want to be able to read E10. This could be the reason for the issue? I am unsure).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    You should not be comparing `fscanf()`'s return value to `EOF`. Re-read the documentation for the function to see what it rerurns and when. – Shawn Nov 12 '19 at 02:03
  • You should include both your actual and expected output, btw. – Shawn Nov 12 '19 at 02:06
  • If you want to read hex, use `%x` instead of `%d`. But — be aware that `%x` will read `10` as decimal 16, not decimal 10. You could try `%i`, but that requires a `0x` or `0X` prefix to identify hexadecimal input (and would treat a leading zero as indicating octal input). You should probably capture and test the result of `fscanf()` more thoroughly. Your first call to `fscanf()` should end up with `printf()` printing `1 0` instead of `10` because the `%c` eats the `1` leaving the `0` for the `%d`. – Jonathan Leffler Nov 12 '19 at 02:50

1 Answers1

0

In general, if you want to read hex, use %x instead of %d. But — be aware that %x will read 10 as decimal 16, not decimal 10. You could try %i, but that requires a 0x or 0X prefix to identify hexadecimal input (and would treat a leading zero as indicating octal input).

You should probably capture and test the result of fscanf() more thoroughly. If you get EOF, so be it; but you might get 1 or 2. (In some situations, you could get 0, but with %c as the first conversion specification, you won't get 0 for a result).

Your first call to fscanf() should end up with printf() printing 1 0 instead of 10 because the %c eats the 1 leaving the 0 for the %d.

Subsequent calls have the %c capturing the newline \n, and then the number.

After you read 18, the next fscanf() returns 1 and has the newline after the 18 in hexe, but it fails to convert the E into a decimal number, so 18 is left in hex1 and it is printed twice. The next call places E into hexe and 10 into hex1, so you see 18E 10 in the output.

The argument is similar for why you get 12 twice. There's a newline after the 12. Again, the %c captures the newline and then the %d conversion would fail (because fscanf() encounters EOF, but it has made one successful conversion, so it doesn't report EOF yet). So hex1 still contains 12, and 12 would therefore be printed twice.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • well.... now I know why... what's the best course to correct it tho so it works properly? –  Nov 13 '19 at 13:19
  • The second paragraph outlines what you need to do. You need to decide what behaviour you want on the first line of input. You need to specify what should happen with the `E10` line and variants such as `1E0` and `Z00` (which is obviously not hex). When the requirements are specified, you can write the code. Think seriously about using `fgets()` to read the line of data and then use `sscanf()` to parse it. That way, you can parse the line different ways if the first attempt doesn’t work. – Jonathan Leffler Nov 13 '19 at 13:27