版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验六 读/写磁盘指定位置信息实验目的:1)了解磁盘的物理组织。 2)掌握windows系统提供的有关对磁盘操作API。 3)根据输入的扇区号读/写指定扇区。实验准备知识:1) 设置读写操作的位置:函数SetFilepointer()用于移动一个打开文件中的读/写指针,这里磁盘设备被当作文件处理,因此用于移动文件读/写指针在磁盘上的位置。2) 读文件:用函数ReadFile()读取磁盘指定区域的内容(从文件指针指示的位置开始读取文件中的数据)。3)写文件: 用函数Write File()将数据写入磁盘指定区域。函数在文件指针所指的位置完成写操作,写操作完成后,文件指针按实际写入的字节数来调整。
2、实验内容:在实验五的基础上,继续完成该试验。编写两个函数,分别完成如下功能。1) 对给定的扇区号读取该扇区的内容。2) 将用户输入的数据写入指定的扇区。实验要求: 深入理解操作系统设备当作文件处理的特性,理解函数SetFilepointer()、ReadFile()及Write File()中每个参数的实际意义并能在本实验中正确应用。实验指导: 在主程序中让用户选择:R、W、Q或,若用户选择R,则调用函数BOOL SectorRead(HANDLEHandle),完成读给指定扇区的功能;若用户选择W,则调用函数BOOL SectorWrite(HANDLEHandle),完成对给定扇区号写入信
3、息的功能,若用户选择Q,则程序退出。参考源代码:/ 操作系统实验六.cpp : Defines the entry point for the console application./#include stdafx.h#include 操作系统实验六.h#include winioctl.h #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE=_FILE_;#endifDISK_GEOMETRY disk_info;HANDLE GetDiskInformation(char drivername);
4、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
5、 R ro read,W to Write,Q to quit!n);Choice=getchar();printf(n);switch(Choice) case W: if(!SectorWrite(Handle) printf(Write Sector Fail!n); getchar(); break; case R: if(!SectorRead(Handle) printf(Read Sector Fail!n); getchar(); break; case Q: exit(0); break; default: printf(Input Error!,Try again plea
6、se!n); getchar(); return nRetCode; HANDLE GetDiskInformation(char drivername) / GetDiskInformation获取磁盘信息 char device=.; device4=drivername; HANDLE FloopyDisk; DWORD ReturnSize;/ DWORD双字节值 DWORD Sector; double DiskSize; FloopyDisk=CreateFile(device, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SH
7、ARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS|FILE_FLAG_NO_BUFFERING, NULL);if(FloopyDisk=INVALID_HANDLE_VALUE) printf(INVALID_HANDLE_VALUE!n);if(GetLastError()=ERROR_ALREADY_EXISTS) printf(Can not Open Disk!%dn,GetLastError();if(!DeviceIoControl(FloopyDisk, IOCTL_DISK_GET_DRIVE_GEOMETRY,
8、NULL, 0, &disk_info, 50, &ReturnSize, (LPOVERLAPPED)NULL) printf(Open Disk Error!%dn,GetLastError();printf(Disk Information:n);printf(t BytePerSector:%dn,disk_info.BytesPerSector);printf(t SectorPerTrack:%dn,disk_info.SectorsPerTrack);printf(t TracksPerCylider:%dn,disk_info.TracksPerCylinder);printf
9、(t Cylider:%dn,disk_info.Cylinders);Sector=disk_info.Cylinders.QuadPart* disk_info.TracksPerCylinder* disk_info.SectorsPerTrack;printf(t There is %d Sectors!n,Sector); DiskSize=Sector*disk_info.BytesPerSector;printf(t Size of Disk:%4.2f KBn,(DiskSize)/(1024*1024);return FloopyDisk;BOOL SectorRead(HA
10、NDLE Handle) char ReadBuffer1024*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 Form:n); scanf(%d,&SectorNumber); printf(n); Sector=disk_info.Cylinders.QuadPart* disk_in
11、fo.TracksPerCylinder* disk_info.SectorsPerTrack; if(SectorNumberSector) printf(There is not this Sector!n); printf(Content:n); BytestoRead=SectorNumber*(disk_info.BytesPerSector); rc=SetFilePointer(Handle,BytestoRead,NULL,FILE_BEGIN); if(!ReadFile(Handle,ReadBuffer,BytestoRead,&BytestoRead,NULL) pri
12、ntf(Read File Error:%dn,GetLastError(); return FALSE; printf(t Text Content:n); for(i=0;i512;i+) printf(%c,ReadBufferi); printf(n); printf(t Hex Text Content:n); for(i=0;iSector)printf(There is not this Sector!n);printf(Please Input the Content to Write to Disk A:n);scanf(%s,&WriteBuffer);/ WriteBuf
13、fer指的是写缓冲区SectorMove=SectorNumber*(disk_info.BytesPerSector);rc=SetFilePointer(Handle,SectorMove,NULL,FILE_BEGIN);if(!WriteFile(Handle,WriteBuffer,512,&BytestoWrite,NULL)printf(Read File Error:%dn,GetLastError();return FALSE;printf(Write Complete!n);return TRUE;实验步骤:1)根据实验五的实验步骤首先新建一个工程文件以及把参考代码输进去,还要在参考代码中需要手动输入头文件包含命令#include winioctl.h。2)因为在参考代码中需要进行读写的盘是软盘A,所以需要把其修改成其他盘。 3)查看运行结果。获取磁盘信息读取磁盘M中扇区号1的内容在磁盘M的扇区1上写入“MENGJI”。读出在M盘1号扇区上新写入的内容MENGJI.退出查看结果窗口实验总结:1)这次实验是在实验五的基础上做的,所以实验
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 产道异常的常见类型
- 五一活动策划方案-登山(3篇)
- 员工活动品牌策划方案(3篇)
- 县市高考应急预案(3篇)
- 护坡抗滑桩施工方案(3篇)
- 斜坡天井施工方案(3篇)
- 景区演出营销方案(3篇)
- 水管气囊施工方案(3篇)
- 浴室渗水施工方案(3篇)
- 烹饪行业应急预案(3篇)
- 2025年特种设备无损检测人员资格考试(无损检测基本知识)历年参考题库含答案详解(5套)
- 《危险化学品目录》(2026版)
- 项目借用资质管理办法
- 心血管-肾脏-代谢综合征(CKM)综合管理中国专家共识2025解读课件
- 2025年山东省青岛市崂山区中考一模语文试题含答案
- 安徽省示范高中皖北协作区高三下学期第27届联考(一模)数学试题
- 建设银行个人贷款合同模板
- 社会体育指导员合作协议
- 《铁路轨道维护》课件-线路基本维修作业工具认识
- 云南省2024年中考物理真题试卷含解析
- 2013年毕业设计任务书
评论
0/150
提交评论