This blog is under construction

Monday, 29 July 2013

C program to delete a file or directory

Write a C program to remove a file or directory.


  #include <stdio.h>
  #include <string.h>
  #define MAX 256

  int main() {
        char filename[MAX];

        /* get the file or directory name from user */
        printf("Enter your file name:");
        fgets(filename, MAX, stdin);
        filename[strlen(filename) - 1] = '\0';

        /* remove the given file or directory */
        remove(filename);

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ls -l
  total 24
  -rwxr-xr-x 1 jp jp 7330 2013-07-29 23:47 a.out
  -rw-r--r-- 1 jp jp   39 2013-07-29 23:15 data.txt
  drwxr-xr-x 2 jp jp 4096 2013-07-29 23:49 done
  drwxr-xr-x 2 jp jp 4096 2013-07-29 23:46 dummy
  drwxr-xr-x 5 jp jp 4096 2013-07-29 22:07 mydir

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your file name:dummy

  jp@jp-VirtualBox:~/$ ls -l
  total 20
  -rwxr-xr-x 1 jp jp 7330 2013-07-29 23:47 a.out
  -rw-r--r-- 1 jp jp   39 2013-07-29 23:15 data.txt
  drwxr-xr-x 2 jp jp 4096 2013-07-29 23:49 done
  drwxr-xr-x 5 jp jp 4096 2013-07-29 22:07 mydir






SEE ALSO

    No comments:

    Post a Comment