You are here:

C++/binary files


Hi,  I would like to make a floppy disk that can put text on the screen before the normal operating system loads.  I have done this all with assembly, but now I need to make a program that will copy this file into the boot sector of my floppy disk.  This is the source code to my transferring program. #include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
 ifstream file ("boot.bin", ios::binary);
 if (file.is_open())
   size = file.tellg();
   memblock = new char [size];
   file.seekg (0, ios::beg); (memblock, size);

   cout << "the complete file content is in memory" << endl;

 else cout << "Unable to open file";
 ofstream drive ("a:", ios::binary | ios::out);
 if (drive.is_open())
   drive.seekp (0, ios::beg);
   drive.write (memblock, size);

   cout << "the file has been copied into boot sector of floppy" << endl;

 else cout << "Unable to open A drive" << endl;
 delete[] memblock;
 return 0;
So I have tried this and it will load the program into memory, but it won't transfer it to the a: drive(floppy disk) because it can't open the a drive.  I don't know how to do this.  I may need to specify a file inside the a drive not just try to open the actual a drive, but I don't know.  Do you have any idea's?  Thank you for all of your help.

The only way to write to the boot sector is with low level sector writing commands.  ofstreams are used to read and write files - which the OS accesses through the file structure of the drive. The OS uses the lower level code to read and write sectors, but it's not available from file level classes.

Here's a link to a C++ class that can read and write floppy sectors. I'm sure there's something there to help you to do it in your program.  


All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2017 All rights reserved.