TAHMID37

append mode

Jul 12th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. /*  TAHMID RAHMAN
  2.     DAMIAN FOREVER
  3.      MATH LOVER
  4.     NEVER GIVE UP
  5. */
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<string.h>
  9. int main(int argc, char *argv[])
  10. {
  11.     FILE *fp;
  12.     char str[255];
  13.     int c;
  14.  
  15.     if(argc!=2)
  16.     {
  17.         printf("Specify file name.\n");
  18.         exit(1);
  19.     }
  20.  
  21.     while(100)
  22.     {
  23.         printf("Which mode?\n1. Write to file\n2. Read from file\n3. Append to file\n");
  24.         printf("Enter choice:");
  25.         scanf("%d", &c);
  26.  
  27.         if(c==1)
  28.         {
  29.             if((fp=fopen(argv[1], "w"))==NULL)
  30.             {
  31.                 printf("The file does not exist!\n");
  32.                 exit(1);
  33.             }
  34.  
  35.             while(*str!='\n')
  36.             {
  37.                 fflush(stdin);
  38.                 printf(":");
  39.                 gets(str);
  40.                 strcat(str, "\n");
  41.                 if(*str!='\n')
  42.                     fputs(str, fp);
  43.             }
  44.             fclose(fp);
  45.         }
  46.         if(c==2)
  47.         {
  48.             if((fp=fopen(argv[1], "r"))==NULL)
  49.             {
  50.                 printf("The file does not exist.\n");
  51.                 exit(1);
  52.             }
  53.  
  54.             while(!feof(fp))
  55.             {
  56.                 fgets(str, 254, fp);
  57.                 if(!feof(fp))
  58.                     printf(str);
  59.             }
  60.             fclose(fp);
  61.         }
  62.         if(c==3)
  63.         {
  64.             if((fp=fopen(argv[1], "a"))==NULL)
  65.             {
  66.                 printf("The file does not exist!\n");
  67.                 exit(1);
  68.             }
  69.  
  70.             while(*str!='\n')
  71.             {
  72.                 fflush(stdin);
  73.                 printf(":");
  74.                 gets(str);
  75.                 strcat(str, "\n");
  76.                 if(*str!='\n')
  77.                     fputs(str, fp);
  78.             }
  79.             fclose(fp);
  80.         }
  81.     }
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment