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.

Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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).

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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 opening and reading files 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