This blog is under construction

Thursday 5 April 2012

remove example in C

Header file:
    stdio.h

Synopsis:
    int remove(const char *file);

Description:
     Removes/deletes the named file and returns 0 on success, non-zero on failure.


remove function C example:


  #include<stdio.h>
  #include<string.h>
  int main() {
        char filename[100];
        int ret = 0;
        printf("Enter the filename to be removed:");
        fgets(filename, 90, stdin);
        filename[strlen(filename) - 1] = '\0';
        ret = remove(filename);
        if (ret == 0)
                printf("Removed successfully\n");
        else
                printf("Task failed\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ cat file.txt
  hello world
  hello world
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the filename to be removed:file.txt
  Removed successfully
  jp@jp-VirtualBox:~/$ ls
  a.out  fclose.c  fflush.c  fflush_sam.c  remove.c


No comments:

Post a Comment