I managed to remove comments froma cpp file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michael8
    New Member
    • Nov 2006
    • 3

    #1

    I managed to remove comments froma cpp file

    The problem was to write a program that reads a .cpp file containing a C++ program and produces a file with all comments stripped from the program.

    I finally got the answer.


    Here it is if someone want it as no one could do it : P



    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    void remove_comments ( ifstream& , ofstream& ); // Function declaration.

    void main()
    {
    string infile, outfile; // declaration of the names of the stream files.
    ifstream source; // declaration of the stream files.
    ofstream target; // declaration of the stream files.


    // To validate the user when entering the name of the file wrong.
    do {

    cout << "Enter Input File Name: ";
    cin >> infile;

    } while ( source.fail() );

    cout << "Enter Output File Name: ";
    cin >> outfile;

    source.open ( infile.c_str() );
    target.open ( outfile.c_str() );


    remove_comments ( source , target); // Calling the function,





    source.close(); // to close the opened files.
    target.close();

    }


    //---------------------------------------------


    // This function takes the two files from the main. It reads line by line from the input file and copy it to the output file eliminating the comments.
    void remove_comments ( ifstream& Source , ofstream& Target)
    {
    string line;
    bool flag = false;


    while ( ! Source.eof() ) // This loop is to get assure that the whole input file is read.
    {


    getline(Source, line); // To read line by line.


    if ( line.find("/*") < line.length() ) // searching for " /* " to eliminat it and all its content.
    flag = true;
    if ( ! flag )
    {
    for (int i = 0; i < line.length(); i++ )
    {

    if(i<line.lengt h())
    if ( ( line.at(i) == '/' ) && ( line.at(i + 1 ) == '/' ) ) // searching for " // " to eliminate all its content.
    break;
    else
    Target << line[i]; // To copy lines in the output file.

    }

    Target<<endl;


    }
    if ( flag )
    { if ( line.find("*/") < line.length() )
    flag = false;
    }


    }
    }
  • Danilorg
    New Member
    • Oct 2008
    • 1

    #2
    Very good.
    I just enhanced it, because your programa was failing when you put "/*" as a string, so it was not a commentary and the program was treating it like commentary.

    I organized it in classes to. Thanks.

    ----------------------------------------------------------------------------------------------------------
    MAIN.CPP

    /* Federal University of Minas Gerais - UFMG
    Engineering School
    Object Oriented Design, Project and Programming
    Practical Work 2;part 1
    Professor: Renato Mesquita
    Group: Danilo Gomes
    Bruno Damiao
    */
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "FFiles.h"

    using namespace std;

    void main()
    { FFiles readFile;

    string infile, outfile; //in and out file name
    ifstream source; // declaration of the stream files.
    ofstream target;
    do{
    cout << "Enter Input File Name: ";
    cin >> infile;
    }while(source.f ail());//while file reading fails, keep asking for input file name

    cout << "Enter Output File Name: ";
    cin >> outfile;

    source.open(inf ile.c_str());
    target.open(out file.c_str());

    readFile.remove _comments(sourc e, target); //Calling the function,

    source.close();//to close the opened files.
    target.close();

    }

    ---------------------------------------------------------------------------------------------------------------
    FFILES.H

    //declaration of FFiles
    #include <fstream>

    using namespace std;

    class FFiles{

    public:

    void remove_comments (ifstream&, ofstream&); // Function declaration.
    void checkCommentsSl ashSlash(string &, bool&, ofstream&);
    void checkComments(s tring&, bool&, ofstream&);
    };

    ------------------------------------------------------------------------------------------------------------------
    FFILES.CPP

    //Definition of FFiles
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include "FFiles.h"

    using namespace std;

    void FFiles::checkCo mmentsSlashSlas h(string& line, bool& flag, ofstream& Target)
    {
    if (!flag)
    {
    for(int i = 0; i < line.length(); ++i )
    {
    if((line.at(i) == '/') && (line.at(i+1) == '/')) // searching for " // " to eliminate all its content.
    break;
    else
    Target << line[i]; // copy the character of the position i to the Target
    }
    Target << endl;
    }
    }//end FFiles::checkCo mmentsSlashSlas h

    void FFiles::checkCo mments(string& line, bool& flag, ofstream& Target)
    {
    int contaAspas = 1; //initialized as 1, because if don't find "/*" will not set flag true
    if (line.find("/*") < line.length()) // searching for " /* " to eliminate it and all its content.
    //the .find searchs in the forward direction for the first occurrence of a substring that matches
    //the specified sequence of characters and returns its position
    {
    contaAspas = 0;
    for(int i = 0; i < line.find("/*"); ++i ){ //check if "/*" is or isn't a commentary
    if((line.at(i) == '"'))
    contaAspas++;
    }
    }
    if((contaAspas % 2) == 0) //in case of even quotation marks it's a commentary
    flag = true;

    checkCommentsSl ashSlash(line, flag, Target); //check the "//" comments

    if (flag)
    {
    if (line.find("*/") < line.length()) //when it finds the end of commentary, it restarts to copy
    flag = false;
    }
    }//end FFiles::checkCo mments


    void FFiles::remove_ comments (ifstream& Source ,ofstream& Target)
    {
    string line; //auxiliar string to store the read line
    bool flag = false;

    while (!Source.eof()) // This loop is to get assure that the whole input file is read.
    {
    getline(Source, line); // To read line by line.
    //checks comments for the read line
    checkComments(l ine, flag, Target);

    }//end while
    }//end FFiles::remove_ comments

    -----------------------------------------------------------------------------------------------------------

    Hope it be helpful!

    Comment

    Working...