By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

I managed to remove comments froma cpp file

P: 3
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.length())
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;
}


}
}
Nov 13 '06 #1
Share this Question
Share on Google+
1 Reply


P: 1
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.fail());//while file reading fails, keep asking for input file name

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

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

readFile.remove_comments(source, 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 checkCommentsSlashSlash(string&, bool&, ofstream&);
void checkComments(string&, bool&, ofstream&);
};

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

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

using namespace std;

void FFiles::checkCommentsSlashSlash(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::checkCommentsSlashSlash

void FFiles::checkComments(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;

checkCommentsSlashSlash(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::checkComments


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(line, flag, Target);

}//end while
}//end FFiles::remove_comments

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

Hope it be helpful!
Oct 3 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.