操作系統(tǒng)實(shí)驗(yàn)(六)-讀寫磁盤_第1頁
操作系統(tǒng)實(shí)驗(yàn)(六)-讀寫磁盤_第2頁
操作系統(tǒng)實(shí)驗(yàn)(六)-讀寫磁盤_第3頁
操作系統(tǒng)實(shí)驗(yàn)(六)-讀寫磁盤_第4頁
操作系統(tǒng)實(shí)驗(yàn)(六)-讀寫磁盤_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、實(shí)驗(yàn)六 讀/寫磁盤指定位置信息實(shí)驗(yàn)?zāi)康模?)了解磁盤的物理組織。 2)掌握windows系統(tǒng)提供的有關(guān)對磁盤操作API。 3)根據(jù)輸入的扇區(qū)號讀/寫指定扇區(qū)。實(shí)驗(yàn)準(zhǔn)備知識:1) 設(shè)置讀寫操作的位置:函數(shù)SetFilepointer()用于移動一個打開文件中的讀/寫指針,這里磁盤設(shè)備被當(dāng)作文件處理,因此用于移動文件讀/寫指針在磁盤上的位置。2) 讀文件:用函數(shù)ReadFile()讀取磁盤指定區(qū)域的內(nèi)容(從文件指針指示的位置開始讀取文件中的數(shù)據(jù))。3)寫文件: 用函數(shù)Write File()將數(shù)據(jù)寫入磁盤指定區(qū)域。函數(shù)在文件指針?biāo)傅奈恢猛瓿蓪懖僮鳎瑢懖僮魍瓿珊?,文件指針按?shí)際寫入的字節(jié)數(shù)來調(diào)整。

2、實(shí)驗(yàn)內(nèi)容:在實(shí)驗(yàn)五的基礎(chǔ)上,繼續(xù)完成該試驗(yàn)。編寫兩個函數(shù),分別完成如下功能。1) 對給定的扇區(qū)號讀取該扇區(qū)的內(nèi)容。2) 將用戶輸入的數(shù)據(jù)寫入指定的扇區(qū)。實(shí)驗(yàn)要求: 深入理解操作系統(tǒng)設(shè)備當(dāng)作文件處理的特性,理解函數(shù)SetFilepointer()、ReadFile()及Write File()中每個參數(shù)的實(shí)際意義并能在本實(shí)驗(yàn)中正確應(yīng)用。實(shí)驗(yàn)指導(dǎo): 在主程序中讓用戶選擇:R、W、Q或,若用戶選擇R,則調(diào)用函數(shù)BOOL SectorRead(HANDLEHandle),完成讀給指定扇區(qū)的功能;若用戶選擇W,則調(diào)用函數(shù)BOOL SectorWrite(HANDLEHandle),完成對給定扇區(qū)號寫入信

3、息的功能,若用戶選擇Q,則程序退出。參考源代碼:/ 操作系統(tǒng)實(shí)驗(yàn)六.cpp : Defines the entry point for the console application./#include stdafx.h#include 操作系統(tǒng)實(shí)驗(yàn)六.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雙字節(jié)值 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指的是寫緩沖區(qū)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;實(shí)驗(yàn)步驟:1)根據(jù)實(shí)驗(yàn)五的實(shí)驗(yàn)步驟首先新建一個工程文件以及把參考代碼輸進(jìn)去,還要在參考代碼中需要手動輸入頭文件包含命令#include winioctl.h。2)因?yàn)樵趨⒖即a中需要進(jìn)行讀寫的盤是軟盤A,所以需要把其修改成其他盤。 3)查看運(yùn)行結(jié)果。獲取磁盤信息讀取磁盤M中扇區(qū)號1的內(nèi)容在磁盤M的扇區(qū)1上寫入“MENGJI”。讀出在M盤1號扇區(qū)上新寫入的內(nèi)容MENGJI.退出查看結(jié)果窗口實(shí)驗(yàn)總結(jié):1)這次實(shí)驗(yàn)是在實(shí)驗(yàn)五的基礎(chǔ)上做的,所以實(shí)驗(yàn)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150