4

I have a file pointer and I want to remove the file after loading the content of the file in a data structure. I'm not sure whether the file should be closed before removing. Which part of the code is correct?

FILE* myFile = fopen("abc.bin", "rb");
/* Code to load contents of file and do some operations */
fclose(myFile);
remove("abc.bin");

OR

FILE* myFile = fopen("abc.bin", "rb");
/* Code to load contents of file and do some operations */
remove("abc.bin")
fclose(myFile);

Or is it fine to use either of them?

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
  • 1
    rename != remove... – user202729 Jan 18 '18 at 07:33
  • 2
    This depends on operating system. Also please don't use multiple language tags, use only the one you're really programming in. – Some programmer dude Jan 18 '18 at 07:34
  • @user202729 Ah. Edited. I was about to remove, not rename –  Jan 18 '18 at 07:35
  • Downvote, you show no research effort. A quick Google turns up "If the file is currently open by this or another process, the behavior of this function is implementation-defined (in particular, POSIX systems unlink the file name although the file system space is not reclaimed until the last running process closes the file; Windows does not allow the file to be deleted)." on [cppreference](http://en.cppreference.com/w/c/io/remove). – user202729 Jan 18 '18 at 07:35
  • 2
    It depends on the o/s — in part. It's a good idea to close the file first, but on Unix/Linux systems, there won't be a problem if it is open. Windows may be fussier. – Jonathan Leffler Jan 18 '18 at 07:35
  • 1
    i dont see any advantage in removing a file without closing it ? what will you achieve even if it is possible to do so? – zappy Jan 18 '18 at 07:38
  • 3
    Rule of thumb: close first, then remove. – Jabberwocky Jan 18 '18 at 07:44
  • 1
    @user202729 The question is short and concrete. How can you be sure that there was no research ? Depending on OSes both could work, so don't you think it is possible that OP asks because he found contradictory information/examples ? I upvote, because when waking up I thought that second would be completely wrong and now, thanks to this question, I know it's not but that first should still remain the preferred way to go :-) – Christophe Jan 18 '18 at 07:55
  • @Christophe ... have research effort, but doesn't put into the question (show)? How would that help? – user202729 Jan 18 '18 at 07:57
  • Possible duplicate of [Is it ‘safe’ to remove() open file?](https://stackoverflow.com/questions/1370549/is-it-safe-to-remove-open-file) – Raedwald Jan 18 '18 at 08:00

1 Answers1

4

It depends on the operating system.

On Windows, for example, the file is not removed:

If the file is currently open by the current or another process, the behavior of this function is implementation-defined (in particular, POSIX systems unlink the file name, although the file system space is not reclaimed even if this was the last hardlink to the file until the last running process closes the file, Windows does not allow the file to be deleted)

Find more detail in this link.

Dean Seo
  • 5,486
  • 3
  • 30
  • 49