blob: 5813a48fb6a19ce3679b865858f1c2ff3dde3e51 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<h1 id="firstHeading" class="firstHeading">remove</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int remove( const char* fname );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Deletes the file identified by character string pointed to by <code>fname</code>.</p>
<p>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).</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> fname </td> <td> - </td> <td> pointer to a null-terminated string containing the path identifying the file to delete </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>0</code> upon success or non-zero value on error.</p>
<h3 id="Notes"> Notes</h3> <p><a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/remove.html">POSIX specifies</a> many additional details for the behavior of this function.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE* fp = fopen("file1.txt", "w"); // create file
if (!fp)
{
perror("file1.txt");
return EXIT_FAILURE;
}
puts("Created file1.txt");
fclose(fp);
int rc = remove("file1.txt");
if (rc)
{
perror("remove");
return EXIT_FAILURE;
}
puts("Removed file1.txt");
fp = fopen("file1.txt", "r"); // Failure: file does not exist
if (!fp)
perror("Opening removed file failed");
rc = remove("file1.txt"); // Failure: file does not exist
if (rc)
perror("Double-remove failed");
return EXIT_SUCCESS;
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Created file1.txt
Removed file1.txt
Opening removed file failed: No such file or directory
Double-remove failed: No such file or directory</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.21.4.1 The remove function (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.21.4.1 The remove function (p: TBD) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.21.4.1 The remove function (p: 302) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.4.1 The remove function (p: 268) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.4.1 The remove function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="rename" title="c/io/rename"> <span class="t-lines"><span>rename</span></span></a></div> </td> <td> renames a file <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/remove" title="cpp/io/c/remove">C++ documentation</a></span> for <code>remove</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/io/remove" class="_attribution-link">https://en.cppreference.com/w/c/io/remove</a>
</p>
</div>
|