0

Is it safe and acceptable to opening a file and immediately close it to simply remove its contents.

FILE *fp = fopen("file_name.dat","w");
fclose(fp);

I try it and works fine for me. what is recommended method? Please explain other way(s) of doing it. Not duplicate of truncate file I want to know is this way to remove a file true because I guess it and not read it in any books

EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • 4
    Possible duplicate of [How to truncate a file in C?](https://stackoverflow.com/questions/873454/how-to-truncate-a-file-in-c) – Federico klez Culloca Jul 03 '17 at 10:35
  • Note: the answers provided only release the storage to the operating system. The contents is still in the disk blocks. To really remove it, open the file in r/w mode and overwrite its current contents with random data. – Paul Ogilvie Jul 03 '17 at 11:27
  • @PaulOgilvie after doing my way on fopen and fclose, i open file_name.txt with regular editor and all its contents are removed. would you please give me more specific information on release the storage to the operating system and data remains on disk blocks. – EsmaeelE Jul 04 '17 at 12:17
  • It is not the file, it is the disk. The disk is managed by the filesystem. Open/close tell the filesystem to do something. In this case the storage (the disk blocks) are _released_. But _released_ is not the same as _erased_. If I would take a disk editor and look at the blocks that your file had used, I would still be able to see the contents. So if your file contained passwords... Threfore, to be sure the contents is erased, first overwrite your current contents. – Paul Ogilvie Jul 04 '17 at 12:55
  • @PaulOgilvie thanks. i guess file is an abstract layer to work with actual disk blocks and in this case we only free the file (a handle to work with blocks) by do nothing on real blocks. if we can access to that specific blocks(by a SW) we can see previous (in file) data which is stored in file and yet still exists on blocks. – EsmaeelE Jul 04 '17 at 13:18
  • @EsmaeelE, thanks for spotting it. I always have problems with my laptop keyboard and my fingers get twisted. – Paul Ogilvie Jul 04 '17 at 16:39

2 Answers2

3

There are multiple ways you can acheive it. It all depends on your use case. Few of them are as follows: 1. As you have suggested but with few checks

FILE *fp = fopen("file_name.dat","w"); /*fopen("file_name.dat","wb");*/
if(fp)
{
    fclose(fp);
}

It is better to check for the fopen return.

  1. You can remove the file altogether and open the file again. https://www.tutorialspoint.com/c_standard_library/c_function_remove.htm
Rajat Kothari
  • 300
  • 1
  • 2
  • 12
  • thanks. I use tutoiralspoint way of remove file. Also by trying another method i open a file for reading fopen ("file.txt","r") and do remove () on it. also this way work for me. – EsmaeelE Jul 04 '17 at 12:48
1

easy way to clear the content of file:

fclose(fopen("file.txt", "w"));
suraj
  • 403
  • 4
  • 15
  • 1
    Although it seem s fast answer, but suppose a condition (i am not any way to occur ) that fopen() cant open the file and return null file pointer http://en.cppreference.com/w/c/io/fopen . i test to pass null ptr in fclose() and give me seg fault. – EsmaeelE Jul 03 '17 at 11:39