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.
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: _O_WRONLY, O_TRUNC, set file size to zero, Truncate Windows file
7 Comments:
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
@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.
hehe automation!
Yeah i understood now.Thats nice to do as you said, it will be very helpful for debugging.
Keep it up da...
Regards,
Arut
@Anonumous:
:-) I too agree this is not an automation. Phrase changed :-)
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 !
Amiable fill someone in on and this mail helped me alot in my college assignement. Gratefulness you as your information.
Post a Comment
<< Home