Tarek B
Tarek B

Reputation: 494

Reading a specific sector from a hard drive using C on Linux (GNU/Linux )

I know that a hard drive are system's files (/dev/sdXX) –then treated like files-, I have questions about this:

  1. I tried those following lines of code but nothing positive

-------first attempt----------

int numSecteur=2;
char secteur [512];
FILE* disqueF=fopen("/dev/sda","r"); //tried "rb" and sda1 ...every thing
fseek(disqueF, numSecteur*512,SEEK_SET);
fread(secteur, 512, 1, disqueF);
fclose(disqueF);

-------Second attempt----------

    int i=open("/dev/sda1",O_RDONLY);
    lseek(i, 0, SEEK_SET);
    read(i,secteur,512);
    close(i);

------printing the results----------

printf("hex : %04x\n",secteur);
printf("string : %s\n",secteur);
  1. Why the size is of the file /dev/sda1 is just 8 KBytes ?

the size !!!

  1. How the data is stored (binary or hex….) "for the printing"

Please, I need some clues, and if someone need more details just he ask.

Thanks a lot.

Ps: Running kali 2 64bits ”debian” on VMware and i am RooT.

Upvotes: 0

Views: 1521

Answers (2)

komar
komar

Reputation: 881

You cannot printf() array of chat like that, you need right print result. For example as hex dump:

for (int i = 0; i < sizeof(secteur); i++) {
    printf ("%02x ", secteur[i]);
    if ((i + 1) % 16 == 0)
        printf ("\n");
}   

Upvotes: 1

  1. I tried those following lines of code but nothing positive

That's not a question.

  1. Why the size is of the file /dev/sda1 is just 8 KBytes ?

That isn't the file size, it's the device number. (There are two parts, so the device number of sda1 is 8,1)

  1. How the data is stored (binary or hex….) "for the printing"

Data isn't "stored for the printing". Data is stored (in electrical voltages representing binary, but you don't need to know that), and you can print it any way you want.

Upvotes: 2

Related Questions