实验十一:读/写磁盘指定位置信息(只需完成读取)

实验十一:读/写磁盘指定位置信息(只需完成读取)

一、实验目的

(1)了解磁盘的物理知识。

(2)掌握Windows系统提供的有关对磁盘操作API。

(3)根据输入的扇区号读/写指定扇区。

二、实验准备

1.设置读/写操作的位置

函数SetFilePointer()用于移动一个打开文件中的读/写指针,这里磁盘设备被当作文件处理,因此用于移动文件读/写指针在磁盘上的位置。

原型:

DWORD SetFilePointer(
   HANDLE hFile,	//打开的文件句柄
   LONG lpdistanceToMove,	//指针要移动的偏移量的低32位,用于指定移动文件指针的字节大小
   PLONG lpDistanceToMoveHigh,	//指针要移动的偏移量的高32位
   DWORD dwMoveMethod		//文件指针移动的初始位置,如图5-2
);

表5-2dwMoveMethod的值

描述
FILE_BEGIN开始点为0或者为文件的开始位置
FILE_CURRENT开始点为文件指针的当前位置
FILE_END开始点为文件的结尾位置
2.读文件

读取磁盘指定区域的内容

原型:

BOOL ReadFile(
	HANDLE hFile,             //要读的文件的句柄
	LPVOID lpBuffer,           //指向文件缓冲区的指针
	DWORD nNumberOfBytesToRead,     //从文件中要读取的字节数
	LPDWORD lpNumberOfBytesRead,    //指向从文件中要读取的字节数的指针
	LPOVERLAPPED lpOverlapped	 //指向OVERLAPPED结构的指针
);

3.写文件

该函数将数据写入磁盘指定区域

BOOL ReadFile(
	HANDLE hFile,            //要读的文件的句柄
	LPVOID lpBuffer,         //指向文件缓冲区的指针
	DWORD nNumberOfBytesToWrite,   //从文件中要读取的字节数
	LPDWORD lpNumberOfBytesWritten,  //指向从文件中要读取的字节数的指针
	LPOVERLAPPED lpOverlapped		//指向OVERLAPPED结构的指针
);

三、实验内容

在本章实验十的基础上,继续完成该实验。完成如下功能:

对给定的扇区号读取该扇区的内容。

四、实验要求

深入理解操作系统将设备当作文件处理的特性,理解函数SetFilePointer()、Readfile()及Writefile()中每个参数的实际意义并能在本实验中正确使用。

五、实验结果

在这里插入图片描述

六、源代码

// Disk_Information.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Disk1.h"
#include "winioctl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

DISK_GEOMETRY disk_info;
HANDLE GetDiskInformation(char drivername);
BOOL SectorRead(HANDLE Handle);
BOOL SectorWrite(HANDLE Handle);
/
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode=0;
	HANDLE Handle;
	char Choice;
	Handle=GetDiskInformation('C');
	while(TRUE)
	{
		printf("please select Read or Write!Input 'R' to read,'Q' to quit!\n");
		Choice=getchar();
		printf("\n");
		switch(Choice)
		{
			
			case 'R':
			{
				if(!SectorRead(Handle))
					printf("Read Sector Fail!\n");
				getchar();
				break;
			}
			case 'Q':
			{
				exit(0);
				break;
			}
			default:
			{
				printf("Input Error!Try again please!\n");
				getchar();
			}
		}
	}

	return nRetCode;
}
HANDLE GetDiskInformation(char drivername)
{
	char device[]="\\\\.\\C:"; 
	device[4]= drivername;
	HANDLE FloopyDisk;
	DWORD ReturnSize;
	DWORD Sector;
	double DiskSize;
	FloopyDisk=CreateFile(device,
		GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		FILE_FLAG_RANDOM_ACCESS|FILE_FLAG_NO_BUFFERING,
		NULL);
	if(FloopyDisk==INVALID_HANDLE_VALUE)
		printf("INVALID_HANDLE_VALVE!\n");
	if(GetLastError()==ERROR_ALREADY_EXISTS)
		printf("Can not Open Disk!%d\n",GetLastError());
	if (!DeviceIoControl(FloopyDisk,IOCTL_DISK_GET_DRIVE_GEOMETRY,
		NULL,
		0,
		&disk_info,
		50,
		&ReturnSize,
		(LPOVERLAPPED)NULL))
		printf("Open Disk Error!%d\n", GetLastError());
	printf("Disk Information:\n");
	printf("\tBytesPerSector:%d\n",disk_info.BytesPerSector);
	printf("\tSectorPerTrack:%d\n",disk_info.SectorsPerTrack);
	printf("\tTrackPerCylinder:%d\n",disk_info.TracksPerCylinder);
	printf("\tCylinder: %d\n", disk_info.Cylinders);
	Sector= disk_info. Cylinders.QuadPart* disk_info. TracksPerCylinder* disk_info. SectorsPerTrack;
	printf("\tThere is %d Sectors!\n", Sector);
	DiskSize=Sector*disk_info. BytesPerSector;
	printf("\tSize of Disk: %4.2fMB\n",(DiskSize)/(1024*1024));
	return FloopyDisk;
}

BOOL SectorRead(HANDLE Handle)
{
	char ReadBuffer[1024*16];
	DWORD SectorNumber;
	DWORD BytestoRead;
	DWORD Sector;
	DWORD rc;
	int i;
	if (Handle==NULL)
	{
	  printf ("There is No disk!\n");
	  return FALSE;
	}

	printf ("Please Input the Sector Number to Read From:\n");
	scanf("%d",&SectorNumber);
	printf ("\n");
	Sector =disk_info.Cylinders.QuadPart*
		disk_info.TracksPerCylinder*
		disk_info.SectorsPerTrack;
	if (SectorNumber>Sector) 
		printf("There is not this Sector !");
	printf("Content:\n");
	BytestoRead=SectorNumber*(disk_info.BytesPerSector);
	rc=SetFilePointer(Handle,BytestoRead,NULL,FILE_BEGIN);
	if (!ReadFile(Handle,ReadBuffer,BytestoRead,&BytestoRead,NULL))
	{
	  printf("Read File Error:%d\n",GetLastError());
	  return FALSE;
	}
	printf("\t Text Content:\n");
	for (i=0;i<512;i++)
	{ 
	  printf("%c",ReadBuffer[i]);
	}
	printf("\n");
	printf("\t  Hex Text Content: \n");
	for (i=0;i<512;i++)
	{
	  printf("%x",ReadBuffer[i]);
	  printf(" ");
	}
	printf("\n");
	return TRUE;
}

七:实验总结

应用程序首先显示软盘的信息,然后提示用户进行选择“Please Select Read Or Write! Input ‘R’ to read,’Q’ to quit!”当用户输入R表示要读软盘后,应用程序提示用户“Please input the Sector Number to Read to:”输入要读的磁盘号,当用户输入2表示要读第2个道的内容,应用程序显示“Content:”后分别以字符形式和十六进制形式显示软盘上第2道的内容。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值