4

Looking for a simple program to a delete a file written in ANSI C.
Just as an example how would you delete a file at "C:\test.txt" with C?

Jim Fell
  • 13,750
  • 36
  • 127
  • 202
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

3 Answers3

7

You can delete a file from the OS using the remove() function. Like so:

#include <stdio.h>
int main(){
    if(remove("HELLO.txt") == -1)
        perror("Error in deleting a file");
    return 0;
}

The remove() function is defined in stdio.h. Here are some docs.

Rivasa
  • 6,510
  • 3
  • 35
  • 64
1

Use the remove function. I believe it is in "stdio.h"

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

remove or unlink

remove is declared in 'stdio.h'

unlink is declared in 'unistd.h'

unlink is posix function, remove is ansi C function. They all work fine in windows.

unlink only delete files, remove can be used to delete directories.

oldmonk
  • 739
  • 4
  • 10