Click here to Skip to main content
11,934,783 members (41,874 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

CFloppyDisk 2.0

, 14 Feb 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
A class for reading and writing floppy disk sectors directly [Win 95/98/ME/NT/2K/XP]

What this class is for?

CFloppyDisk is a class that allows you to read and write physical floppy disk sectors directly. The class provides static member functions for resetting the floppy disk controller, for reading a physical sector and for writing a physical sector. I am using the term, physical sector, to avoid confusion with absolute sectors.

Honestly, these three functions might as well have been put into a single header file and distributed as a sort of source code library. But I made it a class, so that people will find it easier to use it. These days people prefer classes for everything.

Why this class was made?

In Windows NT/2000 we can read and write directly onto a floppy disk by using the CreateFile API call. You can simply open \\.\A: 

Unfortunately this technique will not work under Windows 95, 98 and ME. This probably is an indicator that underneath the GUI, ME is more 98 than 2000 as is popularly believed. Well that shortcoming is now solved. This class will serve you well, I hope.

One class - Any OS

As I just mentioned there is a huge difference in the techniques used for directly accessing a floppy in the NT/2K set of Operating Systems and the Windows 9x/ME set of Operating Systems. With this class, you need not bother with what version of Windows your program will be running on. Internally the class has separate sets for functions, but the user can simply call the generic functions. The class will make the required OS version checks and call the appropriate function internally.


  • You must be running one of Windows 95/98/ME/NT/2K/XP

Using the class.

There are three static functions which you can use. Thus you should not instantiate the class. Simply include the header file and call the static functions directly.


static void ResetDisk()

ResetDisk resets the floppy disk controller and drive. It does not do anything to the floppy disk. But the floppy drive's read/write head will be recalibrated. Normally you might want to start off a series of reads and writes with a controller reset. But you must also use this when you get an error. If you do not reset the controller after an error, then consequent behavior of the disk controller will be random and unpredictable.


static int ReadFloppyDisk(BYTE *buffer, WORD cylinder, WORD sector, WORD head)

ReadFloppyDisk is used to read a single physical sector into a buffer. Floppy disk sectors are 512 bytes for most standard floppies. So buffer should be allocated at least 512 bytes. You might want to find out the number of bytes per sector, that your floppy drive supports. The other parameters are cylinder, sector and head which are the physical parameters required to specify where you want to read the sector from. This function will return 0 for success and non-zero for failure, make sure you check the return value, always.


static int WriteFloppyDisk(BYTE *buffer, WORD cylinder, WORD sector,WORD head)

WriteFloppyDisk is the analogous function to ReadFloppyDisk that does just the opposite. It writes the bytes contained in buffer onto the physical sector specified by cylinder, sector and head. This function will return 0 for success and non-zero for failure, make sure you check the return value, always.

Some points to remember

The floppy drive motor might take some time to catch up with our function calls. These functions will not wait for that to happen. So you must be careful that you do not do successful reads and writes onto the same sector till you are sure the data has been successfully read from or written into the sector in question. Often you will find that the functions return failure. This happens because most floppy drives are now thoroughly unused or over-used and are often in a very bad state. The quality of floppy diskettes also leaves a lot to be desired. The solution is to loop all calls thrice.

For example, if you are performing a read or a write, call the function and if it fails, call it again. Repeat this three times. I have found that three times is a pretty alright number to get a reasonable success rate. Also, it helps if you call the reset function after long operations. And please remember that the floppy drive is the slowest working part in your computer and keep that in mind. What that means is, don't try to rush it. If you are writing critical information into a sector, perhaps for copy protection, then it would be a good idea to use a Sleep in between.

Example usage

void CCFloppyDiskTestDlg::OnButton1() 
    unsigned char buff[512];	
    strcpy((char*)buff,"This is kinda cool");
        MessageBox("write failed");
        MessageBox("read failed");

How to loop calls to improve success rate.

int m_return_code;
int count=0;
    MessageBox("After three attempts, we still failed. Destroy the floppy");

Technical Note regarding ResetDisk

Under Windows NT/2K we cannot directly make Interrupt 13h calls. For the Win 9x/ME set of Operating Systems the class makes a call to Interrupt 13h Function 0h which resets the floppy disk controller. To achieve the same in the Windows NT/2K set of Operating Systems, I have taken a different approach.

Initially I took the mistaken attitude that Windows NT/2K will not have resetting problems, but unfortunately I was wrong. Then I was stuck with the problem of resetting the internal buffers and error flags that NT/2K maintains when a floppy disk is directly opened. That's when I found out that all I had to do was to open the floppy disk and then close it immediately. I tried this out with bad floppies. And my success percentage improved from a very low 20% to almost 95% [I still got the occasional error]. But that can be improved to 99.999% by using the triple-loop method I have mentioned above. I believe what happens is that when we close the file handle, NT/2K flushes its internal buffers. This might internally result in a resetting of the floppy drive. I am not certain about this but that inference seems highly probable.

Version History

  • Version 2.0 - This now supports Windows 95/98/ME/NT/2K/XP
  • Version 1.0 - This supported only Windows 95/98/ME


Todd C. Wilson - It was Todd who suggested that I write a combined version of the class that supports both sets of Operating Systems. I owe version 2.0 to Todd.

Michael Dunn & Tim Smith - As you might observe, if you look through the source code, the entire functionality of the class is implemented via static functions. And I badly wanted some kind of static construction for some of my static members via calls to static functions. Without Tim's and Mike's help, I'd still have been sitting here puzzled and swearing at Microsoft.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

GeneralData copy protection from one CD to anather CD Pin
sushilabhanvar23-Jun-09 21:40
membersushilabhanvar23-Jun-09 21:40 
QuestionHow to detect bad sector in drive.Please help. Pin
want2beboss19-Jul-07 16:44
memberwant2beboss19-Jul-07 16:44 
QuestionOther Removable Dvices Pin
AlexQII20-Apr-07 13:14
memberAlexQII20-Apr-07 13:14 
GeneralRead/Write permission Pin
VC_RYK2-Apr-07 0:51
memberVC_RYK2-Apr-07 0:51 
Questionreading 1024 Byte / 5 sec / 800 KB Pin
dudi_33322-Dec-06 5:08
memberdudi_33322-Dec-06 5:08 
CfloppyDisk works fine with 512 Byte/Sec.
I Need some stuff to read old none standard CP/M 1024B/S / 5 sec / 80 Tack Disks.
Any concept ? Confused | :confused:

Regards and thanks


QuestionFloppies and .net DLL Pin
RealJunk2-Dec-06 21:34
memberRealJunk2-Dec-06 21:34 
GeneralIt doesn't work Pin
fan wei fang28-Dec-05 8:18
memberfan wei fang28-Dec-05 8:18 
GeneralRe: It doesn't work Pin
Nishant Sivakumar29-Dec-05 9:48
staffNishant Sivakumar29-Dec-05 9:48 
GeneralRe: It doesn't work Pin
fan wei fang29-Dec-05 16:01
memberfan wei fang29-Dec-05 16:01 
QuestionCan you help me ???. help me (absread() , abswirte()) Pin
Minh Vuong Tran11-Dec-05 17:35
memberMinh Vuong Tran11-Dec-05 17:35 
AnswerRe: Can you help me ???. help me (absread() , abswirte()) Pin
hans_artmann21-Apr-06 14:07
memberhans_artmann21-Apr-06 14:07 
Generalfloppy or cd-rom Pin
Best_student13-Feb-05 22:58
memberBest_student13-Feb-05 22:58 
GeneralHelp me Pin
sos1184-Dec-04 20:44
membersos1184-Dec-04 20:44 
QuestionHow to find a unused sector in disk? Pin
greenolive_bing14-Oct-04 16:08
membergreenolive_bing14-Oct-04 16:08 
GeneralAccess From Pin
SwaimRM13-Oct-03 7:49
memberSwaimRM13-Oct-03 7:49 
GeneralNice nice Pin
=[ Abin ]=16-Sep-03 15:11
member=[ Abin ]=16-Sep-03 15:11 
Question800kB floppies 80 tracks, 10 sectors - any chance? Pin
hans_artmann16-Aug-03 6:05
memberhans_artmann16-Aug-03 6:05 
GeneralThank you, Nish!!! Pin
PJ Arends11-Aug-03 10:57
memberPJ Arends11-Aug-03 10:57 
GeneralRe: Thank you, Nish!!! Pin
Nishant S11-Aug-03 16:30
editorNishant S11-Aug-03 16:30 
GeneralFloppy write protection Pin
Anonymous12-May-03 6:37
sussAnonymous12-May-03 6:37 
GeneralReading cd sectors Pin
DhruvJayal22-Apr-03 22:17
sussDhruvJayal22-Apr-03 22:17 
QuestionCan you tell me how I can format the floppy disk by programming? Pin
Wei Huang19-Feb-03 1:25
memberWei Huang19-Feb-03 1:25 
GeneralThanks Nish Pin
Andrew Peace1-Sep-02 13:30
editorAndrew Peace1-Sep-02 13:30 
GeneralRe: Thanks Nish Pin
Nishant S1-Sep-02 16:04
editorNishant S1-Sep-02 16:04 
QuestionFormatting a Disk? Pin
Myself26-Jul-02 18:14
memberMyself26-Jul-02 18:14 
AnswerRe: Formatting a Disk? Pin
Nishant S26-Jul-02 19:37
editorNishant S26-Jul-02 19:37 
GeneralRe: Formatting a Disk? Pin
Myself27-Jul-02 9:56
memberMyself27-Jul-02 9:56 
AnswerRe: Formatting a Disk? Pin
Raman deep21-Oct-03 7:26
memberRaman deep21-Oct-03 7:26 
GeneralWrong sector calculation Pin
sanpee26-Apr-02 8:15
membersanpee26-Apr-02 8:15 
GeneralRe: Wrong sector calculation Pin
hans_artmann11-Aug-03 13:43
memberhans_artmann11-Aug-03 13:43 
GeneralCylanders over 80 Pin
Sohail Bakhtiari14-Mar-02 13:23
memberSohail Bakhtiari14-Mar-02 13:23 
GeneralRe: Cylanders over 80 Pin
Anonymous11-Apr-02 0:07
memberAnonymous11-Apr-02 0:07 
GeneralRe: Cylanders over 80 Pin
Nish [BusterBoy]11-Apr-02 1:01
memberNish [BusterBoy]11-Apr-02 1:01 
GeneralRe: Cylanders over 80 Pin
Tom Humphrey12-Jun-02 1:08
memberTom Humphrey12-Jun-02 1:08 
QuestionSample?? Pin
Stephen Woods18-Feb-02 14:24
memberStephen Woods18-Feb-02 14:24 
AnswerRe: Sample?? Pin
Nish [BusterBoy]18-Feb-02 15:25
memberNish [BusterBoy]18-Feb-02 15:25 
QuestionHas anyone run this on XP yet? Pin
Nish [BusterBoy]15-Feb-02 6:23
memberNish [BusterBoy]15-Feb-02 6:23 
AnswerRe: Has anyone run this on XP yet? Pin
David Defoort15-Feb-02 7:51
memberDavid Defoort15-Feb-02 7:51 
GeneralRe: Has anyone run this on XP yet? Pin
Nish [BusterBoy]15-Feb-02 8:00
memberNish [BusterBoy]15-Feb-02 8:00 
GeneralDoesn't work. Pin
Anonymous21-Aug-04 19:19
sussAnonymous21-Aug-04 19:19 
GeneralRequest Pin
Tweety14-Feb-02 22:28
memberTweety14-Feb-02 22:28 
GeneralRe: Request Pin
Mauricio Ritter15-Feb-02 0:35
memberMauricio Ritter15-Feb-02 0:35 
GeneralRe: Request Pin
Nish [BusterBoy]15-Feb-02 1:43
memberNish [BusterBoy]15-Feb-02 1:43 
GeneralRe: Request Pin
Mauricio Ritter15-Feb-02 2:23
memberMauricio Ritter15-Feb-02 2:23 
GeneralRe: Request Pin
Tweety15-Feb-02 1:52
memberTweety15-Feb-02 1:52 
GeneralRe: Request Pin
Nish [BusterBoy]15-Feb-02 1:55
memberNish [BusterBoy]15-Feb-02 1:55 
GeneralRe: Request Pin
Mauricio Ritter15-Feb-02 2:25
memberMauricio Ritter15-Feb-02 2:25 
GeneralRe: Request Pin
Nish [BusterBoy]15-Feb-02 4:22
memberNish [BusterBoy]15-Feb-02 4:22 
GeneralRe: Request Pin
Mauricio Ritter15-Feb-02 2:29
memberMauricio Ritter15-Feb-02 2:29 
GeneralRe: Request Pin
Nish [BusterBoy]15-Feb-02 4:21
memberNish [BusterBoy]15-Feb-02 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 15 Feb 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid