Strip Comments

Write a program to read a file, strip the comments and write the output to another file.

Questions by mehaboob.p

Showing Answers 1 - 6 of 6 Answers

{

FILE *f, *of;

f = fopen("c:/myprog1.cpp", "r");

of = fopen("c:/comments.cpp", "a");

char* ch = new char[2];

*ch = fgetc(f);

int l=0;while (!feof(f))

{

if ((*ch == '/') && (fgetc(f)=='/'))

{

l++;

do

{

*ch = fgetc(f);

fputc(*ch, of);

printf("%d , %c", *ch, *ch);

getchar();

}

while (*ch != 10);

}

*ch = fgetc(f);

}

printf(
"number of comments: %d", l);

getchar();

}

nemuns

  • Feb 2nd, 2009
 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

enum ParseState
 {
  INITIAL,
  ONE_SLASH_READ,
  TWO_SLASHES_READ,
  ONE_SLASH_STAR_READ,
  ONE_SLASH_STAR_AND_STAR_READ
 } CommentParseState;

StripComment()
{
 FILE * fp;
  FILE *fn;
 char ch[1000], buffer[100];;
  int buf,j, i = 0;
  int temp = 0;
  
  CommentParseState  = INITIAL;
   
 fp = fopen("C:\Documents and Settings\Mini\My Documents\Mini\Programs\Prog Usable\hashing.c", "r");
  fn = fopen("C:\Documents and Settings\Mini\My Documents\Mini\Programs\Prog Usable\test.c", "w+a");

 while((buf = getc(fp)) != EOF)
 {
  switch (buf)
  {
   case '/':
    if (CommentParseState == INITIAL)
    {
     CommentParseState = ONE_SLASH_READ;
    }
    else if (CommentParseState == ONE_SLASH_READ)
    {
     //CommentParseState = TWO_SLASHES_READ;
     
     // Read upto the End of File or the End of Line
     while(1)
     {
      buf = getc(fp);
      if ((buf == EOF) || (buf == 10))
      {
       break;
      }
     }                     
         CommentParseState  = INITIAL;      
    }
    else if(CommentParseState == ONE_SLASH_STAR_AND_STAR_READ)
    {
     CommentParseState  = INITIAL;
    }
    break;
    
   case '*':
    if (CommentParseState == ONE_SLASH_READ)
    {
     while(1)
     {
      buf = getc(fp);
      buffer[i] = buf;
      i++;
      if((buf == 42)|| (buf == EOF)) //Check for *
      {
       CommentParseState = ONE_SLASH_STAR_AND_STAR_READ;
       break;
      }
      else if(buf == '/')
      {
       CommentParseState == INITIAL;
       
        for(j=0; j<i; j++)
        {
         temp = buffer[j];
         putchar(temp);
            }
       break;
      }       
     }     
    }
    else
    {
        putchar(buf);
    }
    
     if((buf == EOF))
     {
      putchar('/');
      putchar('*');
      for(j=0; j<i; j++)
     {
      temp = buffer[j];
      putchar(temp);
          }                         
    }

    break;
     
   default:
    if (CommentParseState == INITIAL)
    {
     putchar(buf);
    }
    else if (CommentParseState == ONE_SLASH_READ)
    {
     CommentParseState  = INITIAL;
         putchar('/');
            putchar(buf);            
    }
    else if (CommentParseState == ONE_SLASH_STAR_AND_STAR_READ)
    {
     CommentParseState = INITIAL;
     for(j=0; j<i; j++)
     {
      temp = buffer[j];
      putchar(temp);

     }
    
    }
    break;
   }
    } 
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions