去除C/C++代码中的注释(将注释换为空格)

 

// RemoveComments.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

void removeComments(char* buf,int n);

int main(int argc, char* argv[])
{
//    printf("Hello World!\n");
    string filename = "FileWithComments.cpp";
    ifstream fin;
    int length;
    char * buffer;
    try{
        fin.open(filename.c_str());
    }catch (std::exception &e) 
    {
        cout<<e.what()<<endl;
    }

    if (fin.is_open()) {
        cout<<"Success!"<<endl;
        fin.seekg(0,std::ios::end);
        length = fin.tellg();
        fin.seekg(0,std::ios::beg);

        buffer = new char[length];
        fin.read(buffer,length);
        fin.close();
        buffer[length-1]='\0';
        cout<<buffer<<endl;
        removeComments(buffer,length);
        cout<<"---------------------------------------"<<endl;
        cout<<buffer<<endl;


    }



    return 0;
}

void removeComments(char* buf,int n)
{
    char c;
    char *p,*end;//p遍历字符串,end是终点
    char *sy,*dy;//sy标志双引号,dy标志单引号
    char *xg;//xg标志斜杠/
    char *xgx,*xxg;//xgx标志/*,xxg标志*/
    sy=dy=xg=xgx=xxg=NULL;

    p = buf;
    end = p + n;
    while (p<end) {
        c = *p;
        switch(c) {
        case '"'://记录双引号出现的位置
            {
                if (sy==NULL&&dy==NULL) {
                    sy = p;
                }else{
                    sy = NULL;
                }                
                p++;
            }break;
        case '\''://记录单引号出现的位置
            {
                if (dy==NULL&&sy==NULL) {
                    dy = p;
                }else{
                    dy = NULL;
                }                
                p++;
            }break;
        case '/':
            {
                if (sy==NULL&&dy==NULL) {
                    p++;
                    char c_temp = *p;
                    if (c_temp=='/') {//当前遇到的是"//"
                        *p=' ';
                        *(p-1)=' ';
                        p++;
                        while (*p!='\n') {
                            *p=' ';
                            p++;
                        }
                        /*//"""""'''''
                        *    
                        */
                    }else if (c_temp=='*') {//当前遇到的是"/*"
                        *p=' ';
                        *(p-1)=' ';
                        p++;
                        xgx = p;
                        while (true) {
                            if (*p=='*'&&*(p+1)=='/') {
                                *p=' ';
                                p++;
                                *p=' ';
                                break;
                            }
                            *p=' ';
                            p++;
                        }


                    }else{
                        //nothing
                    }
                }
                p++;
            }break;
        default:
            {
                p++;
            }break;
            
        }
    }
    


}

 

posted on 2014-10-22 16:03  中二向箔  阅读(494)  评论(0编辑  收藏  举报

导航