Remove comments from c/+ + code (change comments to spaces)

Source: Internet
Author: User

//RemoveComments.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include<iostream>#include<fstream>#include<string.h>using namespacestd;voidRemovecomments (Char* BUF,intn);intMainintargcChar*argv[]) {//printf ("Hello world!\n");    stringfilename ="FileWithComments.cpp";    Ifstream fin; intlength; 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]=' /'; cout<<buffer<<Endl;        Removecomments (buffer,length); cout<<"---------------------------------------"<<Endl; cout<<buffer<<Endl; }    return 0;}voidRemovecomments (Char* BUF,intN) {    CharC; Char*p,*end;//p Traversal string, end is end point    Char*sy,*dy;//sy sign Double quote, DY sign single quote    Char*XG;//XG Flag Slash/    Char*XGX,*XXG;//XGX logo/*,xxg logo * /sy=dy=xg=xgx=xxg=NULL; P=buf; End= p +N;  while(p<end) {C= *p; Switch(c) { Case '"'://record the position where double quotes appear            {                if(sy==null&&dy==NULL) {sy=p; }Else{sy=NULL; } P++; } Break;  Case '\ ''://where the single quotation marks appear            {                if(dy==null&&sy==NULL) {dy=p; }Else{dy=NULL; } P++; } Break;  Case '/':            {                if(sy==null&&dy==NULL) {P++; Charc_temp = *p; if(c_temp=='/') {//You are currently encountering the "//"*p=' '; * (P-1)=' '; P++;  while(*p!='\ n') {                            *p=' '; P++; }                        /*// "" "" "" "" ""*/                    }Else if(c_temp=='*') {//You are currently encountering "/*"*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; }    }    }

Remove comments from c/+ + code (change comments to spaces)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.