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).

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now