Program to remove
all kind of Comments (Single line and Multi-Line Comments) from a ‘C’ or same
type of file.
#include <stdio.h>
#include <conio.h>
#include <string.h>
FILE *fp,*fp1;
char fname[15];
int j;
  void
print()
      {
           char
line[200];
           fp=fopen(fname,"r");
           while((fgets(line,
200, fp))!=NULL)
              {
                   printf("%s",line);
              }
           fclose(fp);
      }
  void
Scomment()
      {
           char
line[200];
           int
i=0;
           fp=fopen(fname,"r");
           fp1=fopen("temp.c","w");
           while((fgets(line,
200, fp))!=NULL)
              {
                   for(i=0;line[i]!='\0';i++)
                        {
                            if(line[i]=='/'
&& line[i+1]=='/')
                                {
                                      break;
                                }
                            putc(line[i],fp1);
                        }
              }
           fclose(fp);
           fclose(fp1);
           remove(fname);
           rename("temp.c",fname);
      }
  int
Mcomment(char line[],int k)
      {
                   for(j=k;line[j]!='\0';j++)
                        {
                            if(line[j]=='*'
&& line[j+1]=='/')
                                {
                                      j=j+2;
                                      return
1;
                                }
                        j++;
                        }
               return 0;
      }
  void
main()
      {
           char
line[200];
           int
i=0,k=1;
           clrscr();
           printf("\n\tEnter
Source 'C' Filename : ");
           gets(fname);
           printf("\n\tContents
of the File %s :\n",fname);
           print();
           printf("\nPress
Any Key to Continue : ");
           getch();
           clrscr();
           Scomment();
           fp=fopen(fname,"r");
           fp1=fopen("temp.c","w");
           while((fgets(line,
200, fp))!=NULL)
              {
                   for(i=0;line[i]!='\0';i++)
                        {
                            if(line[i]=='/'
&& line[i+1]=='*')
                                {
                                      k=Mcomment(line,i+2);
                                      i=j;
                                }
                            if(k==0)
                                {
                                      k=Mcomment(line,i);
                                      i=j;
                                }
                            if(k==1)
                                {
                                      putc(line[i],fp1);
                                }
                        }
                   if(k==0)
                        {
                            k=Mcomment(line,0);
                        }
              }
           fclose(fp);
           fclose(fp1);
           remove(fname);
           rename("temp.c",fname);
           printf("\n\tContents
of the File %s :\n",fname);
           print();
           printf("\nPress
Any Key to Continue : ");
           getch();
           clrscr();
      }
 
 
 
No comments:
Post a Comment