?? floppy.cpp
字號:
#include <windows.h>
#include "floppy.h"//函數聲明及其它頭文件包含在在floppy.h中;
#include <stdlib.h>
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("\tBytesPerSector:");
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結構
//的theSupportedGeometry項中(參見FLOPPY.H文件)
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;
//返回disk結構以供后用;
return(theDisk);
}
//讀取特定的磁盤區域的內容并將它們顯示出來(文本和十六進制兩種方式);
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("\tText 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;
}
//從某磁盤扇區中讀出指定扇區里的數據到指定緩沖區RdBuf;
BOOL sectorRead(Disk theDisk,unsigned logSectorNumber,char *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;
}
//從指定緩沖區WrBuf寫從鍵盤得到的數據到某磁盤扇區中;
printf("Please Input the Sector 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的倍數,輸入不足
//此數時系統自己補足(填入WrBuf中的缺省值)
if(!WriteFile(theDisk->floppyDisk ,WrBuf,512,&BytesWrite,NULL))
{
printf("write failed\n");
return false;
}
printf("write complete successfully\n");
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -