C++读取磁盘扇区

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C++读取磁盘扇区

#include
#include
#define headPerCylinder 1023
#define sectorPerhead 63

char * ReadSectors(int cylinder,int head,int sector,int numsec);
char * ReadSectors(int logicalSector,int numsec);

void main()
{
//char *text=ReadSectors(0,0,1,1);//ReadSectors(cylinder,head,sector,numberOfSectors)
char *text=ReadSectors(0,1);
HANDLE filehandle = CreateFile("001",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
DWORD byteswrite;
WriteFile(filehandle,
text,
512*1,
&byteswrite,
NULL);

//cout<}

char * ReadSectors(int cylinder,int head,int sector,int numsec)
{
int logicalSector=cylinder*headPerCylinder*sectorPerhead
+head*sectorPerhead
+(sector-1);

char *buffer=(char*)malloc(512*numsec);
strset(buffer,' ');
//strset功能: 将一个串中的所有字符都设为指定字符
DWORD bytesread;
char devName[] = "////.//PHYSICALDRIVE0";
HANDLE hDevice = CreateFile(devName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Error open disk!","Error",MB_OK);
return NULL;
}
SetFilePointer(hDevice,(logicalSector*512),NULL,FILE_BEGIN);
//if(
ReadFile(hDevice,
buffer,
512*numsec,
&bytesread,
NULL);
/*
)
{
int err;
char error[10];
err=GetLastError();
itoa(err,error,10);
//功能: 把一整数转换为字符串
//用法: char *itoa(int value, char *string, int radix)
MessageBox(NULL,error,"Reading sectors fail!",MB_OK);
return NULL;
}
*/
CloseHandle(hDevice);
return buffer;
}

char * ReadSectors(int logicalSector,int numsec)
{
char *buffer=(char*)malloc(512*numsec);
strset(buffer,' ');
//strset功能: 将一个串中的所有字符都设为指定字符
DWORD bytesread;
char devName[] = "////.//PHYSICALDRIVE0";
HANDLE hDevice = CreateFile(devName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Error open disk!","Error",MB_OK);
return NULL;
}
SetFilePointer(hDevice,(logicalSector*512),NULL,FILE_BEGIN);
//if(
ReadFile(hDevice,
buffer,
512*numsec,
&bytesread,
NULL);
/*
)
{
int err;
char error[10];
err=GetLastError();
itoa(err,error,10);
//功能: 把一整数转换为字符串
//用法: char *itoa(int value, char *string, int radix)
MessageBox(NULL,error,"Reading sectors fail!",MB_OK);
return NULL;
}
*

/
CloseHandle(hDevice);
return buffer;
}

相关文档
最新文档