C unlink
Remove an existing file and check that its name is gone.
A simple example
unlink
- Start with A practice file exists
- Do this Delete that file
- Result The path is absent
Syntax
unlink(preview)
Run these examples in C
Save one example at a time as lesson.c. Use C17 on macOS or Linux with a POSIX shell.
cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lessonClear a disposable print preview
A library kiosk creates a preview whenever a borrower asks for a printout. After printing, the kiosk must remove that scratch file and confirm that the job has finished cleaning up.
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
int main(void) {
char preview[] = "/tmp/print-preview-XXXXXX";
int fd = mkstemp(preview);
if (fd == -1) return 1;
if (close(fd) == -1 || unlink(preview) == -1) return 1;
struct stat info;
if (stat(preview, &info) == -1 && errno == ENOENT) puts("removed");
return 0;
}
removed
The check runs after removal and confirms the preview path is absent.
Prepare a gallery label
A curator stores a short UTF-8 caption beside an exhibit. The desk removes the temporary note after handover. Does its original name still identify an entry?
Prepare a studio handover
A stage manager saves a two-word instruction for the next shift. The desk removes the temporary note after handover. Does its original name still identify an entry?
What to watch for
These C examples use POSIX file APIs on macOS or Linux. Deletion and missing-file behavior vary by API. Limit cleanup to paths created by this job.