C file stream fns (fwrite etc) - do the files move about on disk?

Posted on 2004-08-28
Last Modified: 2010-04-15
Hi all,

if i have a file on the disk and I open it up for writing, if I make some changes to the data, but don't alter the size of the file, does it stay in the same place on the disk? what if the file is fragmented across the disk, does this affect things at all?

thanks for any info.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 11923121
If you just write without appending, the file will stay in the same place (sector), but if you or anybody make a defrag the obviously file will be physically moved.
if you append some data, then is it high probable that this data will not be stored physically continuous.
LVL 23

Expert Comment

ID: 11923139
That depends completely upon the implementation of the underlying filesystem.
ID: 11923467
cheers jaime, sorry should have mentioned I'm talking about FAT, FAT32 and NTFS (I'm assuming they'll be fairly similar in approach).

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

LVL 55

Accepted Solution

Jaime Olivares earned 150 total points
ID: 11923564
talking about FAT/FAT32/NTFS with DOS/Windows I think my comment is still valid. File sectors are not reorganized until an explicit defrag option. Also, files with "system" attribute won't be moved even by defrag.
LVL 22

Expert Comment

ID: 11925228
Why exactly do you want to know if files move around?  The whole point of having a file system is you don't have to worry about where the file is physically stored.

As others have noted, in general files stay put.  But you can't depend on this, it's a undefined aspect of most file systems. There are plenty of things that could trigger a file move:

(1)  If you have any automatically scheduled "defrag" sessions, of course that can move files.

(2)  If the OS thinks that one of the file blocks may be going bad, it might mark that sector bad in the FAT and relocate the file.

(3)  If some program requests a bunch of contiguous space for a file, the OS may move files to accomplish this.

(4)  If the file is on a compressed volume, just changing the data in a block can make the data too large to fit in the old block, requiring the file system to reallocate the blocks.

(5)  There's no telling what various background "helper" and real-time virus checkers may be doing.

ID: 11935825
thanks for the input guys. The points go to jaime (thanks - it's as I figured, I just wanted to hear it from someone else!)

To clarify, what I'm doing is making a prog that wipes files from the disk so that there are no magnetic 'traces' left behind 9there are various commercial apps that do this). I had the idea after reading about 'Spin Rite' by Steve Gibson (great guy, check out his site, lots of interesting projects - ) and its use of 'magnetodynamics' ( ). Basicly different patterns of data on disks can be strong or weak because the magnetic pulses interfere with one another. My prog writes very 'strong' signals to the portion of the disk where an unwanted file exists so that there are no 'traces' left behind. Anyway, this rather requires me to be writing in the same place as the files! Hence my question. Hope that explains things!


Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question