Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was dealing with a prepending problem and suddenly leads me to think about this question....

Maybe not only related to FileStream, I want to know how we actually appends binary into a file physically work.

I guess my question will leads two quetions:

  1. How the hard disk memory is structured?
  2. How file write into physical memory?

Say I have a Text File, contains "ABCDEF", so in physical memory (in hard disk), the memory should contains the binary of the letters "ABCDEF" with file header binary etc (actually no header for plain text file?)....

So will be something like this in physically 6 bytes, orders depends on the processor type:

A        B        C        D        E        F
01000001 01000010 01000011 01000100 01000101 01000110

Say when we want to appends a letter 'G' in the file, so the file will contains 7 bytes:

A        B        C        D        E        F        G

01000001 01000010 01000011 01000100 01000101 01000110 01000111

Both file size in disk physically should 4.00KB.

Because when we write the letter G, is still not excess the 4.00KB, so we have space to write into the 4.00KB memory space.

But when we will appends more things in the file, when execess the 4.00KB will be using 8.00KB.

How does the binary that excess the 4.00KB write into the disk memory?

Does it write a pointer or something to somewhere and declare an index in the disk memory and write the excessing binary into the new memory address?

share|improve this question

migrated from stackoverflow.com Dec 21 '12 at 16:37

This question came from our site for professional and enthusiast programmers.

FileStream has nothing to do with this, the problem is handled directly by the filesystem driver. –  Matteo Italia Dec 21 '12 at 15:16
Didn't expect it will explains this in wikipedia.... So my guess is correct, it fragemented to another address. So there is a index of where the fragemented memory's memory address is also correct I beleive? Then I don't understand why is not existing an operation for Prepending... It should be possible to just add one more memory address as header and point to the begining of file memory address? –  King Chan Dec 21 '12 at 15:22
@MatteoItalia file system driver is what the OS has? Or is something that is from the processor? I am not so familiar with hardware system... –  King Chan Dec 21 '12 at 15:25
@KingChan: it's a driver inside the OS, that provides the "files/directories/..." abstraction over the "sectors" abstraction of the disk. –  Matteo Italia Dec 21 '12 at 16:17
add comment

1 Answer

up vote 4 down vote accepted

Issues like this is why we use OSs, it provides a layer of abstraction so you do not need to concern yourself at the application level about things like how the memory is stored on the hardware level.

The layers are similar to this (for HDD drives), Each layer shows next to it who is responsible for that layer.

        File (File system)
         Bitstream (OS)
    Sectors (Hardware Driver)
     Bits (Hard disk controller)
Magnetic flux (Hard disk controller)

Each layer does not care what the layers above or below it are doing, the only important thing is that it exposes a known API to direct layer above it and it uses a known API to the direct layer below it.

When writing applications you almost never get below the Bitstream layer (System.IO.Stream) when you write your data to the stream it hands it off to the driver and the driver will split the data however it the next layer below it needs it to be split (4k chunks for modern HDDs).

For how the driver tracks "what goes where?" that is up to the driver's implementation.

To actually answer your question, Append works by your program requests the Bitstream for a file. Once it has the bitstream it adds more data to the end of the bitstream. What the OS/Driver does to that bitstream once you write to it is a black box (you don't know the details) and it will be totally different if you are working with a FileStream, MemoryStream, or a NetworkStream. The great thing about this layered structure is you don't need to care! The responsibility is the next layer down's and that layer deals with it.

share|improve this answer
+1, but either I don't understand the diagram or there's something wrong... the streams objects provide an abstraction over files, which are provided by the FS driver, that talks to the disk driver that works with sectors. –  Matteo Italia Dec 21 '12 at 16:21
@MatteoItalia Not all the layers are "abstractions" per say, some are interfaces. A "file" contains one or more streams, the stream provides a abstraction to the storage media beneath it, and so on. All a "file" really is, is a name. You tell the OS you want to access that name and from that you get a file handle that lets you access the stream. Then that is where the "real" abstractions start. –  Scott Chamberlain Dec 21 '12 at 17:17
+1 Very detail explaination! Thanks! So from your explaination, simliarly I can think that a Mouse/Keyboard Driver is also a...layer... a program... that will expose a known API to the OS, so we can do SendKey, Cursor.Position etc simple line of code to control the hardware? –  King Chan Dec 21 '12 at 17:22
@KingChan Exactly, it is just another set of layers, the layer feeding your program mouse movements (The Windows Message Pump in this case) does not know (nor care) if the mouse movement came from a attached USB device or another piece of software on the computer. As long as the layer feeding the message pump follows the API it just passes along the information to the applications that need to know about the movement. –  Scott Chamberlain Dec 21 '12 at 17:25
See this SO question and answer for some hints on why it is hard to prepend –  Scott Chamberlain Dec 21 '12 at 17:57
show 3 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.