-2

I need to write a program using C++ which is able to perform data write/read to/from both random and sequential hard disk sectors.

However, actually I am confused with the term sector and its relation with a file.

What I want to know is, if I simply:

1. Create a string contains word "Hello, world" and then;
2. Save the string into "myfile.txt", 

does the data written in sequential or random sector? If it is sequential (I guess), then how can I write the string to random hard disk sector and then read it again? And also vice-versa.

yunhasnawa
  • 815
  • 1
  • 14
  • 30
  • 1
    That file would be small enough to fit (hopefully) in a single sector... But would this not be *highly* OS and driver dependent? As in, with a RAID 0 setup 2 consecutively written sectors can end up on different disks. – Jongware Oct 25 '14 at 08:16
  • 1
    You are going to corrupt your disk by doing this. You'll render the machine unusable. Don't do that. – David Heffernan Oct 25 '14 at 08:16
  • You can't. There's no relationship between file positions and sectors. To write to a known sector, you need to have special privileges on your computer and use your OS-specific APIs. You will destroy the data on your disk in the process. – n. m. could be an AI Oct 25 '14 at 08:24
  • Whoaa really? I didn't expect that this can cause such a problem like that. Actually this is my school assignment. Perhaps I need to clarify this to my Professor. I can't understand what is the point for gave me this troublesome task. Anyway, thank you very much for your warning and suggestion guys. You really did save my MacBook Pro.. :D – yunhasnawa Oct 25 '14 at 08:34
  • @yunhasnawa As I understand your task, it's simply to output that text in a file. Nothing to do with _sectors_ and such in the 1st place, no need to ask your prof. Just check out the [`c++ file I/O`](http://en.cppreference.com/w/cpp/io/basic_ofstream) standard features, how to do it. – πάντα ῥεῖ Oct 25 '14 at 08:44
  • No. My prof really stated that we should write some megabytes of data to random sectors on a hard disk and then calculate the time the program needed to do that. – yunhasnawa Oct 25 '14 at 08:48
  • 1
    @yunhasnawa The OS will write that data to random sectors, if the data is big enough. You don't have to write to them directly. – πάντα ῥεῖ Oct 25 '14 at 08:54
  • @πάντα ῥεῖ I see. So if the data is larger than maximum hdd's sector size, it will be separated into random sectors right? But we cannot guarantee that the data can be written in sequential sectors. Is that true? – yunhasnawa Oct 25 '14 at 08:59
  • @yunhasnawa Yes, that's correct. – πάντα ῥεῖ Oct 25 '14 at 09:00

1 Answers1

3

What you are trying to do is pretty much impossible today because of file systems. If you want a file (which you seem to), you need a file system. A file system then places the data in some format it wants to the sectors it thinks are best. Advanced filesystems such as btrfs and zfs also do compression, checksumming and placing data on multiple hard disks. So you can't just write to a sector, because you would likely destroy data and you couldn't read it anymore because your file system doesn't understand your data format. Also it wouldn't even know that there is data there because file must be registered in the MFT/btrfs metadata/... tables.

TL;DR Don't try to do it, it will mess up your system and it won't work.

Lorenz
  • 2,179
  • 3
  • 19
  • 18