TAHMID37

stream into

Jul 13th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int main(int argc, char *argv[])
  5. {
  6.     FILE *fp;
  7.     char str[255], *p;
  8.     int i;
  9.  
  10.     if(argc!=2)
  11.     {
  12.         printf("Specify file name.\n");
  13.         exit(1);
  14.     }
  15.  
  16.     if((fp=fopen(argv[1], "w"))==NULL)
  17.     {
  18.         printf("Can't open file.\n");
  19.         exit(1);
  20.     }
  21.  
  22.     printf("Enter text:");
  23.     gets(str);
  24.     p=str;
  25.  
  26.     while(*p)
  27.     {
  28.         if(fputc(*p, fp)==EOF)
  29.         {
  30.             printf("Error writing file.\n");
  31.             exit(1);
  32.         }
  33.         p++;
  34.     }
  35.     fclose(fp);
  36.  
  37.     if((fp=fopen(argv[1], "r"))==NULL)
  38.     {
  39.         printf("Can't open file.\n");
  40.         exit(1);
  41.     }
  42.  
  43.     for(;;)
  44.     {
  45.         i=fgetc(fp);
  46.         if(i==EOF)
  47.             break;
  48.         putchar(i);
  49.     }
  50.     fclose(fp);
  51.     return 0;
  52. }
Add Comment
Please, Sign In to add comment