You are here:

C++/binary files

Advertisement


Question
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);
   file.read (memblock, size);
   file.close();

   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);
   drive.close();

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

   
 }
 else cout << "Unable to open A drive" << endl;
 delete[] memblock;
 system("pause");
 
 
 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.

Answer
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.

http://www.codeproject.com/KB/system/cfloppydisk.aspx  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

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.

Experience

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.

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

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.