0
#include <stdio.h>
#include <stdlib.h>
//#define MAXBUF 1024*1024

int main()
{
    FILE *fp;
    char str[sizeof(MAXBUF)];
    int i;

    fp = fopen("frame1PETData.dat","r");
    if(fp == NULL){
        perror("Error opening the file");
        return -1;
    }
    if(fgets (str,sizeof(MAXBUF),fp)!= NULL)
    {
        puts(str);
        for(i =0;i<60;i++)
        {
            printf("%hhx",str[i]);
        }
    }
    fclose(fp);
    return(0);
}

In the following program, when i try to print the hexadecimal data from the file, the output is in the following way.

enter image description here

But the actual data is as shown below which is different from output data.

a6 d2 fe 44 3c 00 00 ff ff ff ff ff ff ff ff 1a
78 1a 61 1a 80 1a 70 01 0c 1a cf 14 01 11 18 00
b8 05 c0 1c 03 70 15 01 12 89 00 58 06 00 24 01
21 23 01 a5 cb 00 60 06 80 64 01 b2 22 01 a7 8b
00 70 04 c0 22 00 b3 11 01 ad 87 00 d8 10 00 4a
02 24 10 01 ae fa 00 50 04 00 18 01 45 08 02 29
8a 00 90 04 00 2c 02 b6 30 02 29 a5 01 30 08 00

Is my program wrong? And before every byte it is appending ff byte.

jenny
  • 500
  • 7
  • 22
  • You never output any newlines or spaces, so your output contains neither newlines nor spaces. This doesn't seem very surprising. It's also surprising the code compiles — you've commented out the definition of `MAXBUF`. Using `sizeof(MAXBUF)` seems implausible — it is likely to be 4 or 8. You also have problems that your `char` type is signed and your library doesn't seem to be masking negative `char` values to just 1 byte as it should with the `%hhx` format. – Jonathan Leffler Oct 03 '17 at 05:07
  • `sizeof(MAXBUF)`? What the h\*ck is that supposed to do? – melpomene Oct 03 '17 at 05:08
  • 2
    1) `char str[sizeof(MAXBUF)];` --> `char str[MAXBUF];` – BLUEPIXY Oct 03 '17 at 05:09
  • try print with "%x", string[i] & 0xff; Check -> https://stackoverflow.com/questions/8060170/printing-hexadecimal-characters-in-c – Kovinis Oct 03 '17 at 05:09
  • 2
    2) `if(fgets (str,sizeof(MAXBUF),fp)!= NULL)` --> `if(fgets (str, sizeof(str), fp)!= NULL)` – BLUEPIXY Oct 03 '17 at 05:10
  • 2
    3) `for(i =0;i<60;i++) { printf("%hhx",str[i]); }` --> `for(i = 0; str[i]; i++) { printf("%hhx", (unsigned char)str[i]); if((i+1) % 16 == 0) putchar('\n'); else putchar(' '); }` – BLUEPIXY Oct 03 '17 at 05:12
  • Now when I am able to print without ff after doing `string[i] & 0xff` but the data now is different. It is printing in this way... `a6d200689cbe748000180002` but the actual output should be `a6 d2 fe 44 3c 00 00 ff ff ff ff ff ff ff ff 1a 78 1a 61 1a 80 1a 70 01 0c 1a cf 14 01 11 18 00 b8 05 c0 1c 03 70 15 01 12 89 00 58 06 00 24 01 21 23 01 a5 cb 00 60 06 80 64 01 b2 22 01 a7 8b` – jenny Oct 03 '17 at 05:15
  • If `'\x00'` is included in the input data, use `fread` instead of `fgets`. And Set the file open mode to `"rb"` instead of `"r"`. (Also `"%hhx"` --> `"%02hhx"`) – BLUEPIXY Oct 03 '17 at 05:20
  • But when I try to use `for(i =0;i – jenny Oct 03 '17 at 05:25
  • 2
    `sizeof(MAXBUF)` is `sizeof(int)`. If you are using a 32 bit machine, it is usually 4. – BLUEPIXY Oct 03 '17 at 05:28
  • @j.aug BLUEPIXY told you to use `str[i]` as the condition. `sizeof(MAXBUF)` is nonsense. – melpomene Oct 03 '17 at 05:31
  • But i defined my MAXBUF as 1024*1024 ,that means does it provide a size of 1024 Bytes? So sizeof(MAXBUF) meant 1024 right? – jenny Oct 03 '17 at 05:33
  • It(1024*1024) is 1 MByte, not 1024Byte. (Also `sizeof(MAXBUF)` is `sizeof(int)`, not 1MByte). But `fgets` reads only up to linefeed. So Use `fread` instead of `fgets` or each read byte by `fgetc`.Also When `char str[MAXBUF/*1024*1024*/];` , `sizeof(str)` is 1MByte. – BLUEPIXY Oct 03 '17 at 05:42
  • sample of [using fread ver.](https://wandbox.org/permlink/onJpJ4CifPRo2HPT) – BLUEPIXY Oct 03 '17 at 06:20
  • `for(i =0;i<(sizeof(MAXBUF)-1);i++)` Better to use `fread()` and then `i<(sizeof(MAXBUF)-0)` – chux - Reinstate Monica Oct 03 '17 at 13:02

0 Answers0