How to write bits to a disk using C++?

I'm trying to write a program that writes data to a disk in C++ without caring about it's file system. Here is what I can do so far:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
char buffer[] = "Wow! I'm writing this data to a disk without puttting it into a file!";
int main(){
int Disk=open("/dev/sdb",O_RDWR);
write(Disk,buffer,sizeof(buffer));
close(Disk);
return 0;}

But this program can only write ASCII characters to the disk. But what if I want to mainipulate bits on the disk, how would I do that?
The C and C++ file functions work on bytes, which in the language is called a char.

Modify the bits of the characters (bytes) you read and/or write.

Hope this helps.


PS. I don't recommend blithely opening hard drives and writing data to them. There is no such thing as file-system agnostic. Either you obey the filesystem structure or you obliterate it. There is no other option.
How do I modify the bytes of a charecter?

Edit: Sorry. I meant to say "How do I modify the bits of a byte?"

Thank you.
Last edited on
The bits of a byte are modified using the logical bit operators.

http://www.cplusplus.com/doc/tutorial/operators/#bitwise
Topic archived. No new replies allowed.