Thursday, March 20, 2014

C Program to Remove Comments from the C File

C Program to Remove Comments from the File

Description: This program removes comments starting with '//' i.e., single line comments..
You can edit it to remove '--' comments in SQL by just editing the condition..

#include<stdio.h>
#include<ctype.h>
#include<string.h>
 
#define MAXSIZE 1000
 
main()
{
 
 char c, line[MAXSIZE], oline[MAXSIZE];
 int  i=0, linesize=0, doubleq=0;
 
 FILE *ip ,*op;
 
 clrscr();
 
 
 ip = fopen( "input_ph.txt","r");
 op = fopen( "out_ph.txt","w+");
 
 
  while ( (fgets(line,1000,ip)) != NULL )
   {
    linesize = strlen(line);
 
    for(i=0;i<linesize;i++)
    {
     if ( line[i] == '"' )
     doubleq++;            //for validation of '//' in double quotes
 
     if ( !((line[i] == '/') && (line[i+1] == '/')) || (doubleq%2 == 1) )
     oline[i] = line[i];        //copy char by char to OLINE array
 
     else
     {
     oline[i]='\n';
     oline[i+1]='\0';
 
     doubleq = 0;
     break;     //break out from reading char by char once you find comment i.e., '//'
     }
    }
 
    fputs(oline,op);   //put the line truncated from comments to the output file.
    for(i=0;i<linesize;i++)   //null the arrays
    {
     oline[i]=NULL;
     line[i]=NULL;
    }
    i=0;
  }
 
  fclose(ip);
  fclose(op);
 
 getch();
 return 0;
}

No comments:

Post a Comment