Advertisement
Shailrshah

File Operations

Apr 20th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. void copy(char *target_name,char *source_name)
  3. {
  4.     char c;
  5.     FILE *target,*source;
  6.     source=fopen(source_name,"r");
  7.     if(source==NULL)
  8.     {
  9.         printf("\nSource file not found");
  10.     }
  11.     target=fopen(target_name,"w");
  12.     while((c=getc(source))!=EOF)
  13.         putc(c,target);
  14.     printf("\nDone."); 
  15. }
  16.  
  17. void del(char *file_name)
  18. {
  19.     int status;
  20.     status = remove(file_name);
  21.     if(status==0)
  22.         printf("\nDone.");
  23.     else
  24.         printf("File not found");
  25. }
  26.  
  27.  
  28. void rname(char *old_name,char *new_name)
  29. {
  30.     copy(new_name,old_name);
  31.     delete(old_name);
  32. }
  33.  
  34.  
  35. int main()
  36. {      
  37.     char s1[50],s2[50];
  38.     int choice;
  39.     printf("Select your option:-\n1.Copy File\n2.Delete File\n3.Rename File\n");
  40.     scanf("%d",&choice);
  41.     switch(choice)
  42.     {
  43.         case 1: printf("Enter source file name:-\n");
  44.             scanf("%s",s1);
  45.             printf("Enter target file name:-\n");
  46.             scanf("%s",s2);
  47.             copy(s2,s1);        break;
  48.         case 2: printf("Enter file name:-\n");
  49.             scanf("%s",s1);
  50.             del(s1);            break;
  51.         case 3: printf("Enter old name:-\n");
  52.             scanf("%s",s1);
  53.             printf("Enter new name:-\n");
  54.             scanf("%s",s2);
  55.             rname(s1,s2);       break;
  56.         default:printf("Error. Enter option again.\n");
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement