Jinal
Jinal

Reputation: 81

C program to remove comments from same file

Following is the code for removing comments from a C program but the code with removed comments is stored in another program.

#include<stdio.h>

main(int argc , char *argv[])
{
    FILE *fp;
    char ch;
    fp=fopen(argv[1],"r");
    fp1=fopen(argv[2],"w");

    while(1)
    {
        ch=fgetc(fp);
        if(ch==EOF)
                break;
        else
        {
                if(ch=='/')
                {
                        ch=fgetc(fp);
                        if(ch=='/')
                        {
                                while(1)
                                {
                                        ch=fgetc(fp);
                                        if(ch=='\n')
                                                goto label;
                                }
                        }
                        if(ch=='*')
                        {
                                while(1)
                                {
                                        ch=fgetc(fp);
                                        if(ch=='*')
                                        {
                                                ch=fgetc(fp);
                                                if(ch=='/')
                                                {
                                                        while(1)
                                                        {
                                                                ch=fgetc(fp);
                                                                goto label;
                                                         }
                                                }
                                                else
                                                        printf("*");
                                        }
                                }
                        }
                        else
                                printf("/");
                }
        }
        label:
                fputc(ch,fp1);
    }
    fclose(fp);
    fclose(fp1);
}

Now I want to make a program that removes the comment from same file.So when we open it comments should not exist.Kindly guide me because I am not getting any idea how to make such program?

Upvotes: 0

Views: 5247

Answers (2)

user3121023
user3121023

Reputation: 8308

Try this. I tested it on itself and seems to work. It uses different file names so if things go badly the original is still there.

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

int main (int argc , char *argv[]) {
    FILE *fp;
    FILE *fp1;
    int c;
    int c1;
    int previous = 0;
    int literal = 1;

    if ( argc != 3) { // check for number of command line arguments
        printf ( "invalid command syntax\n");
        exit(1);
    }

    if ( strcmp ( argv[1], argv[2]) == 0) { // arguments are the same
        printf ( "use different file names please\n");
        exit(2);
    }

    fp=fopen(argv[1],"r");
    if ( fp == NULL) {
        printf ( "could not open read file\n");
        exit(3);
    }
    fp1=fopen(argv[2],"w");
    if ( fp1 == NULL) {
        printf ( "could not open write file\n");
        fclose(fp);
        exit(4);
    }

    while ( ( c = fgetc ( fp)) != EOF) { // read file to end of file
        if ( c == '"') {
            if ( !( ( previous == '\\') || ( previous == '\''))) { // was previous an escaped or single quoted quote
                literal = !literal;
            }
        }
        if ( c == '/' && literal) {
            if ( ( c1 = fgetc ( fp)) == '/') {
                // single line comment
                while ( ( c = fgetc ( fp)) != '\n') {
                    ; // blank statement
                    // read to end of line
                    // c will be a newline
                }
            }
            else {
                if ( c1 == '*') {
                    /* block comment spanning
                    more than one line */
                    while ( c = fgetc ( fp)) { // keep reading block
                        if ( c == '*') { // may have found the end of block
                            if ( ( c = fgetc ( fp)) == '/') { // yes it is the end of the block
                                c = '\n'; // set c to newline
                                break; // get out of this loop
                            }
                        }
                    }
                }
                else { // this was not a comment
                    fputc ( c, fp1); // write c to the file
                    c = c1; // another character was read so set c to the other character
                }
            }
        }
        previous = c;
        fputc ( c, fp1); // write c to the file
    }

    fclose(fp); // close the files
    fclose(fp1);

    return 0;
}

Upvotes: 1

David Ranieri
David Ranieri

Reputation: 41065

This is not a simple task, using fgetc your code fails on things like:

char *s = "Comments starts with /*";

if its not an exercise and you are using gcc, I suggest to skip comments using fpreprocessed flag:

gcc -fpreprocessed -P -dD -E test.c > cleancode.c

And then get differences (comments) with diff:

diff -ignore-all-spaces test.c cleancode.c > comments.c

Upvotes: 3

Related Questions