⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 floppy.cpp

📁 1 程序开发步骤如下 (1) 打开vc
💻 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 + -