Advertisement
NPantev

Untitled

Jun 26th, 2022
780
137 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Задача 11*. Напишете програма, която да премахва всички коментари в
  2. една С програма.*/
  3.  
  4. #include <stdio.h>
  5.  
  6. int main (int argc, char *argv[])
  7. {
  8.   FILE *fp, *ft;
  9.   char ch, nextc;
  10.   if (argc < 3)
  11.   {
  12.        printf ("No file name given");
  13.   }
  14.   fp = fopen (argv[1], "r");
  15.   ft = fopen (argv[2], "w");
  16.   if (fp == NULL)
  17.        printf ("Opening error");
  18.   if (ft == NULL)
  19.        printf ("Opening error");
  20.   nextc = fgetc (fp);
  21.   while (nextc != EOF)
  22.   {
  23.      ch = nextc;
  24.      nextc = fgetc (fp);
  25.  
  26.      if ((ch == '/') && (nextc == '*'))
  27.      {
  28.         ch = fgetc (fp);
  29.         nextc = fgetc (fp);
  30.         while (!((ch == '*') && (nextc == '/'))) /* unroll until the end of comment*/
  31.         {
  32.           ch = nextc;
  33.           nextc = fgetc (fp);
  34.         }
  35.         nextc = fgetc (fp);
  36.         continue;
  37.      }else if((ch=='/') && (nextc == '/')) // block to handle single line comment.
  38.      {
  39.         nextc = fgetc (fp);
  40.         while (!(nextc == '\n')){
  41.            nextc = fgetc (fp);
  42.         }
  43.        nextc = fgetc (fp);
  44.        continue;
  45.      }
  46.      putc (ch, ft);
  47.    }
  48.   fclose (fp);
  49.   fclose (ft);
  50.   return 0;
  51.  }
Advertisement
RAW Paste Data Copied
Advertisement