Wednesday, February 18, 2015

Learning C Functions : absread ( Dos Only )


C Programming :
Function : absread 

Syntax :
 #include <dos.h>
int absread(int drive, int nsects, long lsect, void *buffer);

Description :
 absread reads specific disk sectors. It ignores the logical structure of a disk and pays no attention to files , FATs, or directories .
absread uses DOS interrupt 0x25 to read specific disk sectors.

drive      drive number to read (0 = A, 1 = B, etc.)
nsects   number of sectors to read
lsect       beginning logical sector number
buffer   memory address where the data is to be read

The number of sectors to read is limited to 64K or the size of the buffer, whichever is smaller .
Return Value
If it is successful, absread returns 0 .

Example :

On error, the routine returns -1 and sets the global variable errno to the value returned by the system call in the AX register.
/*  absread example  */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <ctype.h>

#define SECSIZE 512

int main(void)
{
   unsigned char buf[SECSIZE];
   int i, j, sector, drive;
   char str[10];
   printf("Enter drive letter: ");
   gets(str);
   drive = toupper(str[0]) - 'A';
   printf("Enter sector number to read: ");
   gets(str);
   sector = atoi(str);
   if (absread(drive, 1, sector, &buf) != 0) {
       perror("Disk error");
       exit(1);
   }
   printf("\nDrive: %c   Sector: %d\n", 'A' + drive, sector);
   for (i = 0; i < SECSIZE; i += 16) {
      if ((i / 16) == 20) {
         printf("Press any key to continue...");
         getch();
         printf("\n");

      }
      printf("%03d: ", i);
      for (j = 0; j < 16; j++)
         printf("%02X ", buf[i+j]);
      printf("\t");
      for (j = 0; j < 16; j++)
         if (isprint(buf[i+j]))
            printf("%c", buf[i+j]);
         else printf(".");
      printf("\n");
   }
   return 0;
}

No comments:

Post a Comment