When you fopen a file in C, and you write to it, how is there memory waiting for the additional data? What happens if the file doesn't have free space after it?

1 Answer 1


First, I assume that you are referring to the memory on the hard drive (or other storage medium). When a file is created, a fixed size block is allocated. This will vary by file system and hard drive, but for our discussion, it would typically be 4096 bytes per block, your mileage may vary. The file is then written to that block until it fills up.

Here's where the fun begins. When the current block is full, the computer (OS, file system and hard drive or other media working together) will allocate another block. If the next sequential block is available, it will be used. If it is already in use, then another block somewhere else will be used. The file system will keep track of all of the blocks for a given file, they do not need to be sequential.

This is also the definition/explanation of a fragmented drive. As more and more files are written, altered, appended to and deleted, more files become split between non-sequential blocks. The more this happens, the more fragmented the drive becomes. Defragmenting a drive means rewriting all the files into sequential blocks. These are faster and more efficient because the drive head doesn't have to move from track to track to get to the next blocks in a file.

  • Thanks! When blocks are allocated, are they tracked with a pointer from the previous block? Or is there an index somewhere?
    – jaredcohe
    Commented Apr 18, 2015 at 13:34
  • It all depends on the specific file system and it's a long explanation. Suggest you google "file systems" and look for a Wikipedia page.
    – Cliff B
    Commented Apr 18, 2015 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .