#include //说明sdk的头文件
//#include"floppy.h"
#include
#include
typedef struct disk //Disk是结构体指针
{
HANDLE floppyDisk;
DISK_GEOMETRY theSupportedGeometry;
}*Disk;
//以下为程序中用到的四个函数
Disk openfloppy(char driveLetter);
//打开软盘,并获取相关物理信息,存入返回的一个disk结构的theSupportetry项中
char interwindow(); //功能选择接口
bool physicalDisk(Disk theDisk); //将获得磁盘的物理参数显示出来
bool sectorDump(Disk theDisk); //读取特定的磁盘区域的内容并将他们显示出来(文本和十六制两种方式)
BOOL sectorRead(Disk theDisk,unsigned logSectorNumber,char*RdBuf); //从某磁盘扇区中读出指定字节数量的数据到指定缓冲区RdBuf
BOOL sectorWrite(Disk theDisk); //从指定缓冲区WrBuf写指定字节数量的数据到某磁盘扇区中;
void main()
{
Disk theDisk=NULL;
char choice;
choice=interwindow();
while(choice!='4') //每一次循环中实现对磁盘的一次操作
{
if (theDisk==NULL)
{ theDisk=openfloppy ('a');
}
if (choice=='1')
{ if((physicalDisk(theDisk))==false)
printf("Open Floppy Error!\n");
}
else if(choice=='2')
{
if ((sectorWrite(theDisk))==false)
printf("Write to Floppy Error!\n");
} else if(choice=='3')
{
if((sectorDump(theDisk))==false)
printf("Read from Floppy Error!\n");
} else printf("wrong choice\n");
printf("press any key to return\n");
getchar(); choice=interwindow();
}
if(theDisk!=NULL)
CloseHandle(theDisk->floppyDisk);
{
char interwindow() //选择功能接口
{
system("cls");
char choice='1';
printf("\n\n\n\n\n *********disk I/O test*********\n\n");
printf(" push 1 to get the information of disk\n\n");
printf(" push 2 to write information to a sector \n\n");
printf(" push 3 to read a sector from disk\n\n");
printf(" push 4 to exit from the test\n\n");
printf(" Your choice is: ");
choice=getchar();
getchar();
return choice;
}
bool physicalDisk(Disk theDisk)
{
if (theDisk==NULL)
{
printf("there is no disk available!\n");
return false; } printf("Disk Infomation:\n"); //显示信息 printf("\tButesperSector:");
printf("%d\n",theDisk->theSupportedGeometry.BytesPerSector);
printf("\tSectorsperTrack;");
printf("%d\n,theDisk->theSupportedGeometry.SectorsperTrack");
printf("\tTracksperCylinder:");
printf("%d\n",theDisk->theSupportedGeometry.TracksPerCylinder);
printf("\tCylinders:");
printf("%d\n",theDisk->theSupportedGeometry.Cylinders);
printf("\n\n");
return true;
}
Disk openfloppy(char driveLetter)
{ Disk theDisk;
char buffer[]="\\\\.\\::";
buffer[4]=driveLetter;
theDisk=(struct disk *)malloc(100
DWORD ReturnSize;
HANDLE floppyDisk;
floppyDisk=CreateFile (buffer,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,
FILE_FLAG_RANDOM_ACCESS|FILE_FLAG_NO_BUFFERING,NULL); //打开磁盘
if(!DeviceIoControl(floppyDisk,IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&(theDisk->theSupportedGeometry),50,&ReturnSize,NULL)) //获取它的物理参数
{
return NULL;
}
theDisk->floppyDisk=floppyDisk;
return(theDisk); //返回disk结构以供后用
}
bool sectorDump(Disk theDisk) //读取特定的磁盘区域的内容并将他们显示出来(文本和十六进制两种方式)
{
char RdBuf[512];
int logSectorNumber;
if(theDisk==NULL)
{
printf("there is no disk available!\n");
return false;
}
printf("please Input the Sector NO to read from:"); //从磁盘某扇区中读出内容并显示(文本和十六进制两种方式)
scanf("%d",&logSectorNumber);
printf("\n");
printf("Content:\n");
if(!sectorRead(theDisk,logSectorNumber,RdBuf))
{
printf("Errors Occurred while Reading the Sector!\n");
return false;
}
printf("\t Text content!\n");
for(int i=0;i<512;i++)
{
printf("%c",RdBuf[i]);
} printf("\n");
printf("\tHex Content:\n");
for(i=0;i<512;i++)
{
printf("%x",RdBuf[i]);
printf(" ");
}
printf("\n");
getchar();
return true;
}
BOOL sectorRead(Disk theDisk,unsigned logSectorNumber,char*RdBuf)
//从某磁盘扇区中读出指定山区里的数据到指定缓冲区RdBuf
{
DWORD BytesRead;
long
sectortomove=logSectorNumber*(theDisk->theSupportedGeometry.BytesPerSector);
SetFilePointer(theDisk->floppyDisk,sectortomove,NULL,FILE_BEGIN);
if(!ReadFile(theDisk->floppyDisk,RdBuf,512,&BytesRead,NULL))
{
return FALSE;
}
return true;
}
BOOL sectorWrite(Disk theDisk)
//将用户输入的数据写到指定的磁盘扇区中去
{
DWORD BytesWrite;
char WrBuf[512];
int logSectorNumber;
if(theDisk==NULL)
{
printf("there is no disk available!\n");
return false;
}
printf("Please Input the Secor NO to write to :\n(press enter for end)");
scanf("%d",&logSectorNumber);
getchar();
printf("\n");
printf("Please input the content to write to disk A:\n");
gets(WrBuf);
//当向WrBuf处输入的字节数超过该数组长(此为512),则系统会报错
long
sectortomove=logSectorNumber*(theDisk->theSupportedGeometry.BytesPerSector);
SetFilePointer(theDisk->floppyDisk,sectortomove,NULL,FILE_BEGIN);
//下面的WriteFile的第三个参数只能取512的倍数,输入不足
if(!WriteFile(theDisk->floppyDisk,WrBuf,512,&BytesWrite,NULL))
//此数时,系统自己不足(填入WrBuf中的默认值)
{ printf("write failed\n");
return false;
}
printf("write coplete successfully\n");
return true;
}