C ++ program to remove comments

I am trying to create a program that takes a text file of C ++ code and displays another file with this code, minus any comments it contains.

Assuming rFile and wFile are defined as follows:

ifstream rFile; // File stream object for read only
ofstream wFile; // File stream object for write only

rFile.open("input.txt", ios::in);
wFile.open("output.txt", ios::out);

My first thought was to just go through the text and do the equivalent of pen-up (logo link) when (slightly improved) peek () identifies / * and writes the pen when it sees * /. Of course, after seeing // it will be a "pen-up" until it reaches \ n.

The problem with this approach is that output.txt does not include any of the source spaces or newlines.

This was the code (I didn’t even try to delete the comments at this stage):

while (!rFile.eof())
{
rFile>>first;  //first is a char
wFile<<first;
}

, getline(), endl wFile. , , .

, , - - . ( !)

N.B. , , ++, C.

+3
11

UPDATE:

- , , get, , , " → ".

:

, , getline().

, " → ", "< <". , "endl" . .

, , .

, "\ r\n" "//".

+5

++ ? .

+2

istreambuf_iterator:
.

, .

#include <iterator>
#include <iostream>
#include <algorithm>


class CommentFilter
{
    public:
        CommentFilter(std::ostream& output)
            :m_commentOn(false)
            ,m_output(output)
        {}

        // For each character we find call this method 
        void operator()(char c) const
        {
            // Check for a change in the comment state. (ie PenDown)
            // Leaving this for you to do.


            // Now print the stuff you want.
            if (!m_commentOn)
            {
                // If the commentOn is true then we don't print.
                // Otherwise we do.
                m_output << c;
            }
        }
    private:
        bool            m_commentOn;
        std::ostream&    m_output;
};

int main()
{
    CommentFilter   filter(std::cout);

    // The istreambuf_iterator allows you to iterate through a stream one obejct at a time.
    // In this case we define the object to be a char.
    //
    // So for each obejct (char) we find we call the functor filter with that object.
    // This means filer must have a method so that it can be called like this  filter('a')
    // To-Do this we define the operator() see-above.
    std::for_each(  std::istreambuf_iterator<char>(std::cin),
                    std::istreambuf_iterator<char>(),
                    filter
                );
}
+2
+1

char bool. bool , , , .

char, "".

///* ( "/*Abc*/" ), .

, . */ bool false.

+1

>> . , . get() , getline() .

, .

pen-up, pen-down . , .

, , . ('//' , , .) , a \" ??/" . : , \ ??/, . ( , ).

( ), . , - , , , , , .

, , READING_ALONG, /. SAW_A_SLASH. *, C_STYLE_COMMENT; /, CPP_STYLE_COMMENT, "/" READING_ALONG.

+1

, :

  • ,
  • ,
  • , //
  • , /*
  • , , \ ,

, :

include <stdio>;
INT someVariable = 0;
/* where does this comment end? *\
///  I don't know
someVariable = 6;  
// most text editors don't think it ends until here --> */\
   but someVariable = 6;  shouldnt actually be commented out, and this line should be! \
this is also part of the comment ,   a "3 line " one line comment? WTF!
std::cout << someVariable << std::endl;
// even though "someVariable=6" appears to be commented out, it shouldn't be.
// so this will print "6"

// /* \
*/this text should be commented out aswell 

:

include <stdio>;
INT someVariable = 0;
someVariable = 6;  
std::cout << someVariable << std::endl;

- , orignal, .

+1

, STL , :

find_last_off

find_first_of

, . "\n" , .

anderstornvig, TR1, ++ ( Visual ++ 2008, - g++, Boost).

.

:

"//" ";" "//" ($ )

, . /* .. .

++ TR1

(C)

0

, , ++, , Perl Python . C ++ - .

:

  • ' *\/\/.*' , //,
  • , , , /* , -, . , /* .

: 1. , , . .

0

: -)..

#include <stdio.h>


FILE *rfd,*wfd;
char ch;

void end()
{
    int c=0;
    switch((ch=fgetc(rfd)))
    {
    case '/':
            do
            {
                ch=fgetc(rfd);
                if(ch=='\n')
                    break;
            }while(ch!=EOF);
            ch=fgetc(rfd);
            return;     

    case '*':do
            {
                c++;
                ch=fgetc(rfd);
                if(ch=='*' && (fgetc(rfd))=='/')
                    break;
            }while(ch!=EOF);            
            if(ch==EOF)
                fseek(rfd,-c-1,2);
            ch=fgetc(rfd);
            return;

    default:
        fputc('/',wfd);
        return;
    }
}

int main (int argc,char **argv)
{

    rfd=fopen("read.txt","r");
    wfd=fopen("write.txt","w");

    while((ch=fgetc(rfd))!=EOF)
    {
        if(ch=='/')
                end();

        fputc(ch,wfd);
    }

    printf("\ndone ");
    fflush(stdin);
    getchar();
}
0

If you just want to strip // style comments from lines:

line.erase(line.find('//'));

does the trick.

0
source

Source: https://habr.com/ru/post/1710940/


All Articles