Code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define BYTE unsigned char
#define WORD unsigned int
#define DWORD unsigned long
//dinh nghia cau truc cua mot sector
typedef struct BOOT_SECTOR
{
BYTE First[0x0B];
WORD SectSize;
BYTE ClustSize;
WORD ReservedSect; //so sector truoc bang fat
BYTE FATNum;
WORD RDETSize;
WORD VolSize;
BYTE MediaDescriptor; //ki hieu loai voi
WORD FATSize;
WORD TrackSize;
WORD HeadNum;
DWORD HiddenSect;
DWORD VolSize_;
BYTE Last[512 - 0x24];
}boot;
void main()
{
BOOT_SECTOR bs;
int Csvol;
printf("\nNhap vao chi so vol(0:A,1:B,2:C.........):");
scanf("%d",&Csvol);
if(absread(Csvol,1,0,&bs) != 0)
{
printf("loi truy xuat.chuong trinh ket thuc");
return;
}
//vi tri bat dau cua cluster 2007
WORD vitri;//kieu 2 bytes thi co the ko du de dien gia tri - vol co the chiem nhieu trieu sector
vitri = bs.ReservedSect + bs.FATNum*bs.FATSize + bs.RDETSize*32/bs.SectSize + (2007 - 2)*bs.ClustSize;
//co the kiem tra cluster co ton tai hay ko
BYTE *buf;
buf = new BYTE[bs.ClustSize*512];
//co the kiem tra mang byf co cap phat duoc ko
//doc cluster 2007 va luu vao buf
if(absread(Csvol,bs.ClustSize,vitri,&buf) != 0)
{
printf("\n Loi ko the truy xuat dia. chuong trinh ket thuc");
return ;
}
// hien thi noi dung ra man hinh
printf("Noi dung cua cluster:\n");
for(int i=0;i<bs.ClustSize*512;i++)
printf("%02x",buf[i]);//hoac la printf("%c",buf[i]);
// luu noi dung vao tap tin
char filename[60];
printf("nhap ten can luu noi dung:");
fflush(stdin);
gets(filename);
FILE *f = fopen(filename,"wb");
if(f == NULL)
{
printf("\n Loi Ko the mo tap tin.chuong trinh ket thuc ");
return ;
}
fwrite(buf,1,bs.ClustSize*512,f);
fclose(f);
}