Loading

Archive for 31 Oct 2012

C Program to Remove Comments from C Program File

C - Program to Remove Comments (Single and MultiLine)  from a C - Program File




#include<stdio.h>
#include<conio.h>

void main()
{
  FILE *p;
  char *c,s;
  int i=0,j,k;
  p=fopen("filename.c","r");
  if(p==NULL)
  {
    printf("\Cannot Open Input File");
  }

  s=fgetc(p);
  while(!feof(p))
  {

    if(s=='/')
    {
      s=fgetc(p);
      if(s=='/')
    while((s=fgetc(p))!='\n')
    ;
      else if(s=='*')
    while((s=fgetc(p))!='/')
    ;
      else
      ;

    }
    else
    {
    c[i]=s;
    i++;

    }
    s=fgetc(p);
  }

  c[i]='\0';

  fclose(p);
  p=fopen("filename.c","w");
  i=0;
  while(c[i]!='\0')
  {
    fputc(c[i],p);
    i++;
  }
  fclose(p);

}

Posted in | Leave a comment