Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6334317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:38:11+00:00 2026-05-24T18:38:11+00:00

What’s a good way to replace single-line // input number comments with multi-line /*

  • 0

What’s a good way to replace single-line // input number comments with multi-line /* input number */ comments?

I don’t have any preference for the language used to accomplish the task; I was thinking of Perl or sed. The source language will be C (ANSI X3.159-1989).

Simple scripts like

while(<>) {
  if (m#^(.*?)//#) {
    print $1;
  } else {
    print $_;
  }
}

would be fooled by strings containing // and are not OK. Similarly, // inside a multi-line comment should be left alone.

Edit: Code can assume that there are no trigraphs.


This is the opposite of replace C style comments by C++ style comments. It is similar to Replacing // comments with /* comments */ in PHP (though the accepted answer there cannot handle the special cases I mentioned and so is arguably wrong).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:38:13+00:00Added an answer on May 24, 2026 at 6:38 pm

    You can use boost::wave lexer’s output to replace all the c++ style comments to C style comments. without getting bothered about the edge cases.

     #include <iostream>
     #include <fstream>
    
     #include <boost/wave/cpplexer/cpp_lex_token.hpp>
     #include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
    
     typedef boost::wave::cpplexer::lex_token<> token_type;
     typedef boost::wave::cpplexer::lex_iterator<token_type> token_iterator;
     typedef token_type::position_type position_type;
    
     int main()
     {
           const char* infile = "infile.h";
         const char* outfile = "outfile.h";
           std::string instr;
           std::stringstream outstrm;
           std::string cmt_str;
           std::ifstream instream(infile);
           std::ofstream outstream(outfile);
    
           if(!instream.is_open()) {
                   std::cerr << "Could not open file: "<< infile<<"\n";
               }
         if(!outstream.is_open()) {
             std::cerr << "Could not open file: "<< outfile<<"\n";
         }
    
               instream.unsetf(std::ios::skipws);
               instr = std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
                                            std::istreambuf_iterator<char>());
    
               position_type pos(infile);
               token_iterator  it = token_iterator(instr.begin(), instr.end(), pos,
                boost::wave::language_support(boost::wave::support_cpp|boost::wave::support_option_long_long));
               token_iterator end = token_iterator();
    
               boost::wave::token_id id = *it;
    
          while(it!=end) {
             //here you check the c++ style comments 
             if(id == boost::wave:: T_CPPCOMMENT) {
                std::cout<<"Found CPP COMMENT";
                cmt_str = it->get_value();
                cmt_str[0] = '/';
                cmt_str[1] = '*';
                //since the last token is the new_line token so replace the new line
                cmt_str[cmt_str.size()-1] = '*';
                cmt_str.push_back('/');
                //and then append the newline at the end of the string 
                cmt_str.push_back('\n');
                outstrm<<cmt_str;
             }
             else {
               outstrm<<it->get_value(); 
             }
             ++it;
             id = *it;
         }
         outstream<<outstrm;
    
         return 0;
    }
    

    For further documentation please see:
    http://www.boost.org/doc/libs/1_47_0/libs/wave/index.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.