that is, when CreateFile open the file name designation: "\\.\device" is OK.
because the code is relatively short, so I do not make comments, I believe you can see the code to understand the meaning.
also read and write here is floppy disk a disk NO. 0 sector, if you want to read and write other sectors, you can use the API SetFilePointer.
The method of reading the sector (the following code demonstrates reading data from the No. 0 sector of a drive and writing to the file Boot.bin):
#include <windows.h>
#include <assert.h>
#include <stdio.h>
void Main ()
{
HANDLE hfile;
hfile = CreateFile ("\\\\.\\a:",
Generic_read,
File_share_read,
NULL,
open_existing,
0,
NULL);
assert (hfile && "CreateFile failed!");
pbyte pbuffer = (pbyte) malloc (+);
assert (pbuffer && "Allocate memory failed!");
DWORD Dwlen;
ReadFile (hfile, pbuffer, &dwlen, NULL);
FILE * FP;
fp = fopen ("Boot.bin", "WB");
assert (FP && "Open file failed!");
fwrite (pbuffer, 1, FP);
fclose (FP);
CloseHandle (hfile);
Free (pbuffer);
}
so the corresponding, the method of writing the sector is this way (the following code demonstrates reading data from Boot.bin to the No. 0 sector of the drive):
#include <windows.h>
#include <assert.h>
#include <stdio.h>
void Main ()
{
HANDLE hfile;
hfile = CreateFile ("\\\\.\\a:",
Generic_write,
File_share_write,
NULL,
open_existing,
0,
NULL);
assert (hfile && "CreateFile failed!");
pbyte pbuffer = (pbyte) malloc (+);
assert (pbuffer && "Allocate memory failed!");
FILE * FP;
fp = fopen ("Boot.bin", "RB");
assert (FP && "Open file failed!");
fread (pbuffer, 1, FP);
fclose (FP);
DWORD Dwlen;
WriteFile (hfile, pbuffer, &dwlen, NULL);
CloseHandle (hfile);
Free (pbuffer);
}
Use CreateFile, ReadFile, WriteFile to read and write absolute sectors under Windows NT/2000/XP