Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

remove — removing a file

The remove function from <stdio.h> removes access to a file under the specified name. It returns zero on success and a nonzero value on failure.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdio.h>
int remove(const char *filename);

A successful call to remove(filename) makes the file inaccessible under the name supplied by the string filename. A later attempt to open that name can still succeed, for example if another existing file has been renamed to the now-vacant name.

Check the result against 0; do not assume failure specifically means -1. Behavior for an open file is implementation-defined, so close the stream before removal.

The example creates an empty file using "wx", closes it, and removes it. Run it in an isolated test directory.

Examples

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    const char *name = "cktis-remove-demo.tmp";
    FILE *file = fopen(name, "wx");
    if (file == NULL) {
        fputs("create failed\n", stderr);
        return EXIT_FAILURE;
    }
    if (fclose(file) != 0) {
        fputs("close failed\n", stderr);
        return EXIT_FAILURE;
    }
    if (remove(name) != 0) {
        fputs("remove failed\n", stderr);
        return EXIT_FAILURE;
    }
    puts("removed");
    return EXIT_SUCCESS;
}

Result

removed

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources