Hello>>>

How are you all?
I need a quick help with my assignment... it is in C not in C++... So, Could you help me?
I don't know what to do!!

the question is:
Write a program that reads a data file containing a C program and writes it to another file removing all comments.

the rubbish answer that i made :( :

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

void main ()
{
    FILE *ptr;
    FILE *ptr2;
    char *com = { "*/" };
    char str[500], str2[500];

    if( ( ptr = fopen( "program.dat", "w" ) ) == NULL )
        printf( "\n Failed to open the file" );
    else
    {
    printf( "\nEnter a C program:" );
    fprintf( ptr, "%s", str );
    }

    strcpy( str2, str );

    if( ( ptr = fopen( "program.dat", "r" ) ) == NULL )
        printf( "\n Failed to open the file" );

    else
    {
        while( !feof( ptr ) )
        {
            for( int i=0; str[i]!= NULL; i++ )
                if( ( ptr2 = fopen( "newfile.dat", "w+" ) ) == NULL )
                    printf( "\n Failed to open the file" );

                else
                {
                    if( str[i]=='/' && str[i+1]=='*' )
                    {
                        com = strstr( str, com );
                        while( com != NULL )
                        {
                            com = strstr( com+1, str );
                        }
                    }

                    else
                        fprintf( ptr2, "%c", str2[i] );
                }
            }
    }

}

I found this problem of yours harder than expected. Firstly, I'm better at C++ libraries than the C libraries. So I had to revise all my C File I/O. Secondly, I hate parsing. Anyway here's my solution to your problem.

My technique isn't perfect.

It removes all C-style comments. (/* */) but if it encounters the comments in a string (which is rare in real practical life), it will ignore anything between them in the string.

Plus, I have little experience in parsing text files, so the way I did it may seem messy.

I have posted the code below. The code in HTML format can be viewed here if you prefer syntax highlighting.

/*
+-----------------------------------------------------------------+
| Author&#58; Lim Chong Liang, Andrew                                 |
| Filename&#58; comment.cpp                                           |
| Description&#58; This program takes a C source file, and copies the |
|              text into a new .txt file, with all the comments   |
|              removed. The program only removes C-style          |
|              comments and by default copies itself.             |
| Copyright&#58; Lim Chong Liang, Andrew, 2003                        |
| Website&#58; http&#58;//www.abdn.ac.uk/~u02cll2/                        |
| Email&#58; demandred90@hotmail.com OR u02cll2@abdn.ac.uk            |
+-----------------------------------------------------------------+
*/


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

void copyFileWithoutComments&#40;char&#91;&#93;, char&#91;&#93;&#41; ;

/*
 * The main function.
 */
int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
  char fileToRead&#91;&#93;  = "comment.c" ;
  char fileToWrite&#91;&#93; = "comment.txt" ;
  copyFileWithoutComments&#40;fileToRead,fileToWrite&#41; ;
  printf&#40;"'%s' has been copied to '%s', with all C-style comments removed.\n", 
         fileToRead, fileToWrite&#41; ;
  system&#40;"PAUSE"&#41;;	
  return 0;
&#125;

/*
 * Copies a C source file to a new text file, with all C-style comments removed.
 * fileToRead  - The name of the C source file.
 * fileToWrite - The name of the new text file.
 */
void copyFileWithoutComments&#40;char fileToRead&#91;&#93;, char fileToWrite&#91;&#93;&#41; 
&#123;
  FILE * fRead ;
  FILE * fWrite ;

  /* buffer for a line */
  char lineBuffer&#91;256&#93; ;

  /* open file for reading */
  fRead = fopen&#40;fileToRead,"r"&#41;;
  
  /* stop reading if file pointer is NULL */
  if &#40;fRead == NULL&#41;
  &#123;
    printf&#40;"Could not open file for reading."&#41; ;
    return ;
  &#125;
  
  /* open file for writing */
  fWrite = fopen&#40;fileToWrite,"w"&#41; ;
  if &#40;fWrite == NULL&#41;
  &#123;
    printf&#40;"Could not create file for writing."&#41; ;
    return ;
  &#125;
    
  /* holds character read */
  char next ;

  /* to check if currently in a comment */
  int inComment = 0 ;

  /* keep reading until end of file */
  while &#40; &#40;next = fgetc&#40;fRead&#41;&#41; != EOF &#41;
  &#123;
    /* We are NOT within a comment. */
    if &#40; !inComment &#41;
    &#123;
      if &#40; next == '/' &#41; &#123;
        char temp = fgetc&#40;fRead&#41; ;
        if &#40; temp == '*' &#41;
          inComment = 1 ;
        else  &#123;
          fputc&#40;next,fWrite&#41; ;
          fputc&#40;temp,fWrite&#41; ;
        &#125;
      &#125;
      else
        fputc&#40;next,fWrite&#41; ;
    &#125;
    /* We are in a comment. */
    else
    &#123;
      if &#40; next == '*' &#41;  &#123;
        char temp = fgetc&#40;fRead&#41; ;
        if &#40; temp == '/' &#41;
          inComment = 0 ;
      &#125;
    &#125;
  &#125;
  
  /* close files */
  fclose&#40;fWrite&#41; ;
  fclose&#40;fRead&#41; ;
&#125;

Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks alot...

I don't know how can I reward you?????

THANX :) :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.