Home | Techie Talk | Story Time | About | Contact

Friday, January 16, 2009

How to Truncate Windows file to zero Size?


 
 
Deleting (or) Truncating a Windows File to zero size (especially when the file is in use) is not an easy task in Windows. One will have to close the application (which is using the file), modify the file and then restart the application again.

Just to make this boring task simpler, I have written a c program that will truncate windows file to zero size, even if the file is being used by some other application.

Nothing complex in this code, I have used the "_O_WRONLY | O_TRUNC" options to open the file, which will set the file size to zero.

The absolute path of the File, should be given as the argument to the program.


#include
#include
#include
#include
#include

int main(int argc, char* argv[])
{
if(argc>1)
{
int fileHandler;
fileHandler = _open( argv[1], _O_WRONLY | O_TRUNC );
if( fileHandler == -1 )
{
printf( "\nOPERATION FAILED\n" );
}
else
{
printf( "\nOPERATION SUCCESS\n" );
_close( fileHandler );
}
}
}


Labels: , , ,

7 Comments:

Blogger Arul said...

I am not understanding the purpose of making the file size to zero!!!
Making file to zero size means, what happens to the content inside the file.>>??

Need more real time example!!!

Thanks
Arut

9:27 PM  
Blogger Vetty Officer said...

@Arul :

The file contents will be deleted. I always do this for the log files. To search for an error message in the log file, if the old contents are not removed, I will have to scroll till the end of the file .

If the contents are deleted, the new errors will logged at the top of the file.

12:55 AM  
Anonymous Anonymous said...

hehe automation!

1:22 AM  
Blogger Arul said...

Yeah i understood now.Thats nice to do as you said, it will be very helpful for debugging.

Keep it up da...

Regards,
Arut

10:31 PM  
Blogger Vetty Officer said...

@Anonumous:

:-) I too agree this is not an automation. Phrase changed :-)

11:13 PM  
Anonymous Anonymous said...

What is so great about truncate, is that you don't need file permissions to remove the file, and then create a new file giving it the same permissions as the old one manually !

12:04 PM  
Anonymous Anonymous said...

Amiable fill someone in on and this mail helped me alot in my college assignement. Gratefulness you as your information.

11:26 AM  

Post a Comment

Links to this post:

Create a Link

<< Home