C Program - How to validate a specific string in a file -


please bear me i'm still new c programming, goal serial number saved in file called mtd0 , validate serial number. in bash, command is:

dd if=/dev/mtd0 bs=1 skip=$((0x1fc30)) count=16 2>/dev/null 

and output should be:

1866203214226041 

but want in pure c language, have tried far this:

#include <stdio.h> #include <string.h>  int main () {     file *fp;     file *s;      fp = fopen("/tmp/mtd0","rb");     if(null == fp) {         printf("\n cannot open file!!!\n");         return 1;     }     typedef unsigned char byte;      byte s_no[16];     fseek(fp, 0x1fc30, seek_set);     fread(s_no, 1, 16, fp);     printf ("serial number: %s\n", s_no);     fclose(fp);      char mtd0[16];     char defser[16];     int ret;      memcpy(mtd0, s_no, 16);     memcpy(defser, "1866203214226041", 16);      ret = memcmp(mtd0, defser, 16);      if(ret == 0)     {         printf("serial number correct!\n");     }     else      {         printf("serial number not correct\n");     }      return(0); } 

but when execute that, doesn't print anything. mtd0 not ordinary text file, don't know it's called file looks this please download file if need more info. how can fix code above ?

there few things wrong you're doing.

  1. fseek adjusts file pointer next read or write file; doesn't retrieve file you. furthermore, valid values third argument fseek are:

    • seek_set - set position of file pointer beginning of file plus value in second argument

    • seek_cur - set position of file pointer current position of file pointer, plus value in second argument

    • seek_end - set position of file pointer end of file.

  2. assuming meant "read 16 bytes file @ position 0x1fc30", want save data byte array, not string:

    typedef unsigned char byte;  byte s_no[16]; fseek(fp, 0x1fc30, seek_set); fread(s_no, 1, 16, fp); 
  3. while strcmp correct way compare strings in c, if serial number stored bytes, i'm assuming is, because /dev/mtd0 looks binary file, have compare memory using memcmp byte array set valid value serial.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -