john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file remove c comments

A quick program to remove multi-line comment strings from a file, it removes / foobar / and replaces it with // so not fully debugged ;)

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

/* reads from the file pointer with a lookahead pointer to find a 2 character match */
void readuntilmatch( FILE* ifp , char stopchar , char stopcharsecond )
{
  char prev = getc( ifp );
  char c = prev;
  do{
    c = prev;
    prev = getc( ifp );

    }while( (c != EOF) && (prev != stopchar) && (c != stopcharsecond) );
}

int main( int argc, char* argv[] )
{
  char c;
  char prev;

  FILE* ifp;
  FILE* ofp;

  if( argc != 3 )
  {
    printf("Usage: %s inputfilename outputfilename\n", argv[0]);
    exit(1);
  }
  else
  {
    ifp = fopen( argv[1],"r" );
    ofp = fopen( argv[2],"w" );

    if( ifp == NULL || ofp == NULL )
    {   printf("error opening file\n");
        exit(1);
    }
    else
    {
      c = getc( ifp );
      prev = c;
      do{
          if( prev == '/' && c == '*' )
          {   readuntilmatch( ifp , '*' , '/' );
          }
          else
          {   putc(c, ofp);
          }

          prev = c;
          c = getc( ifp );

        }while( (c != EOF) );

        fclose( ifp );
        fclose( ofp );
    }

  } //end if argc incorrect

    return 0;
} //end main

  • « atmos upload
  • Centos GLIBCXX 3.4.9 not found »

Published

Nov 28, 2011

Category

c

~139 words

Tags

  • c 95
  • characters 5
  • comments 7
  • file 92
  • remove 16