实验六 读/写磁盘指定位置信息
实验目的:1)了解磁盘的物理组织。
2)掌握windows系统提供的有关对磁盘操作API。 3)根据输入的扇区号读/写指定扇区。 实验准备知识:
1) 设置读写操作的位置:
函数SetFilepointer()用于移动一个打开文件中的读/写指针,这里磁盘设备被当作文件处理,因此用于移动文件读/写指针在磁盘上的位置。 2) 读文件:
用函数ReadFile()读取磁盘指定区域的内容(从文件指针指示的位置开始读取文件中的数据)。 3)写文件:
用函数Write File()将数据写入磁盘指定区域。函数在文件指针所指的位置完成写操作,写操作完成后,文件指针按实际写入的字节数来调整。 实验内容:
在实验五的基础上,继续完成该试验。编写两个函数,分别完成如下功能。 1) 对给定的扇区号读取该扇区的内容。 2) 将用户输入的数据写入指定的扇区。 实验要求:
深入理解操作系统设备当作文件处理的特性,理解函数SetFilepointer()、ReadFile()及Write File()中每个参数的实际意义并能在本实验中正确应用。 实验指导:
在主程序中让用户选择:R、W、Q或,若用户选择R,则调用函数BOOL SectorRead(HANDLEHandle),完成读给指定扇区的功能;若用户选择W,则调用函数BOOL SectorWrite(HANDLEHandle),完成对给定扇区号写入信息的功能,若用户选择Q,则程序退出。
参考源代码:
// 操作系统实验六.cpp : Defines the entry point for the console application. //
#include \
#include \操作系统实验六.h\#include \
#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);
- 1 -
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(\Choice=getchar(); printf(\switch(Choice) {
case 'W': {
if(!SectorWrite(Handle)) printf(\ getchar(); break; }
case 'R': {
if(!SectorRead(Handle)) printf(\ getchar(); break; }
case 'Q': {
exit(0); break; } default: {
printf(\ getchar(); }
- 2 -
} }
return nRetCode; }
HANDLE GetDiskInformation(char drivername) // GetDiskInformation获取磁盘信息 {
char device[]=\ device[4]=drivername; HANDLE FloopyDisk;
DWORD ReturnSize;// DWORD双字节值 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(\ALID_HANDLE_VALUE!\\n\if(GetLastError()==ERROR_ALREADY_EXISTS) printf(\if(!DeviceIoControl(FloopyDisk,
IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
&disk_info, 50,
&ReturnSize,
(LPOVERLAPPED)NULL))
printf(\printf(\
printf(\printf(\printf(\printf(\Sector=disk_info.Cylinders.QuadPart* disk_info.TracksPerCylinder* disk_info.SectorsPerTrack;
printf(\
- 3 -
DiskSize=Sector*disk_info.BytesPerSector;
printf(\return FloopyDisk; }
BOOL SectorRead(HANDLE Handle) {
char ReadBuffer[1024*16]; DWORD SectorNumber; DWORD BytestoRead; DWORD Sector; DWORD rc; int i;
if(Handle==NULL) {
printf(\ return FALSE; }
printf(\ scanf(\
printf(\
Sector=disk_info.Cylinders.QuadPart* disk_info.TracksPerCylinder* disk_info.SectorsPerTrack;
if(SectorNumber>Sector) printf(\ printf(\
BytestoRead=SectorNumber*(disk_info.BytesPerSector); rc=SetFilePointer(Handle,BytestoRead,NULL,FILE_BEGIN);
if(!ReadFile(Handle,ReadBuffer,BytestoRead,&BytestoRead,NULL)) {
printf(\ return FALSE; }
printf(\ for(i=0;i<512;i++) {
printf(\ }
printf(\
printf(\ for(i=0;i<512;i++) {
printf(\
- 4 -
printf(\ }
printf(\ return TRUE; }
BOOL SectorWrite(HANDLE Handle) {
char WriteBuffer[1024];
DWORD SectorNumber,SectorMove; DWORD BytestoWrite; DWORD Sector; DWORD rc;
if(Handle==NULL) {
printf(\return FALSE; }
printf(\scanf(\printf(\
Sector=disk_info.Cylinders.QuadPart* disk_info.TracksPerCylinder* disk_info.SectorsPerTrack;
if(SectorNumber>Sector)printf(\printf(\scanf(\指的是写缓冲区 SectorMove=SectorNumber*(disk_info.BytesPerSector); rc=SetFilePointer(Handle,SectorMove,NULL,FILE_BEGIN); if(!WriteFile(Handle,WriteBuffer,512,&BytestoWrite,NULL)) {
printf(\return FALSE; }
printf(\return TRUE; }
实验步骤:1)根据实验五的实验步骤首先新建一个工程文件以及把参考代码输进去,还要在参考代码中需要手动输入头文件包含命令#include \。
- 5 -