C程序从同一文件中删除注释

时间:2014-07-07 09:23:10

标签: c file-io comments

以下是从C程序中删除注释的代码,但删除了注释的代码存储在另一个程序中。

#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);
}

现在我想创建一个程序来删除同一个文件中的注释。所以当我们打开它时,注释不应该存在。请指导我,因为我不知道如何制作这样的程序?

2 个答案:

答案 0 :(得分:3)

这不是一项简单的任务,使用fgetc代码会失败,例如:

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

如果它不是练习而你使用gcc,我建议使用fpreprocessed标记跳过评论:

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

然后与diff获得差异(评论):

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

答案 1 :(得分:1)

试试这个。我自己测试它似乎工作。它使用不同的文件名,因此如果情况严重,原件仍然存在。

#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;
}