操作系统实验11 读/写磁盘指定位置信息(只需完成读取)

这篇博客详细介绍了进行操作系统实验的过程,重点在于读取磁盘上的指定位置信息。实验分为四个部分:实验目的、实验准备、实验内容(包括实验内容的详细说明和代码实现)以及实验结果与总结。

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

一、实验目的

(1)了解磁盘的物理知识。
(2)掌握Windows系统提供的有关对磁盘操作API。
(3)根据输入的扇区号读/写指定扇区。

二、实验准备

 SetFilePointer()   设置读/写操作的位置:函数SetFilePointer()用于移动一个打开文件中的读/写指针,本实验中磁盘设备被当作文件处理,因此用于移动文件读/写指针在磁盘上的位置。
 ReadFile()         读文件:读取磁盘指定区域的内容
 Writefile( )       写文件:该函数将数据写入磁盘指定区域

三、实验内容

(一)实验内容

 在本章实验一的基础上,继续完成该实验。编写两个函数,分别完成如下功能:
     对给定的扇区号读取该扇区的内容。
     将用户输入的数据写入指定的扇区。

(二)代码

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

#include "stdafx.h"
#include "winioctl.h"
#include "zuoye11.h"

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


/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

DISK_GEOMETRY disk_info;
HANDLE GetDiskInformation(char drivername);
BOOL SectorRead(HANDLE Handle);
BOOL SectorWrite(HANDLE Handle);


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");
	//打印16进制编码内容
	printf("\t  Hex Text Content: \n");
	for (i=0;i<512;i++)
	{
	  printf("%x",ReadBuffer[i]);
	  printf(" ");
	}
	printf("\n");
	return TRUE;
}

四、实验结果与总结

    本实验依然需使用数据结构DISK_GEOMETRY,因此同样要将#include "winioctl.h"加入到实验程序的头文件说明中。
    本实验是在实验10的基础上进行修改,新定义了获取磁盘指定位置信息的函数。

结果如下图:
image

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值