Fo C++ Comments I got this little guy working
Code:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <iterator>
using std::ofstream; using std::ifstream;
using std::string;
using std::cout; using std::cin; using std::endl;
int main()
{
string c_comm = "/*";
string comm = "//";
ifstream infile("Daniel.txt");
ofstream outfile("Out.txt");
string line, no_comm;
string::iterator found;
while(getline(infile, line))
{
//if comm not found, 'found' points to endl
found = std::search(line.begin(), line.end(), comm.begin(), comm.end());
no_comm.clear();
no_comm.append(line.begin(), found);
outfile << no_comm << endl;
}
infile.close();
outfile.close();
return 0;
}
input
Code:
Daniel.txt
{
ifstream infile("Test.txt");
ofstream outfile("Test2.txt");
string str, search_start="/*", search_end = "*/";
string :: size_type siz;
bool comment = false;//flag that indicates if line is commented
//read line
while (getline(infile, str))
{
//write line to file if there is no markers /*, */ or line is not commented
if (str.find("/*") == string :: npos && str.find("*/") == string :: npos && !comment)
{
outfile << str << endl;
}
else //line is commented
{
comment = true;
siz = str.find("*/");//try to find end of comment
if (siz != string :: npos)
{
//close comment part and continu, read next line
comment = false;
continue;
}
else
{
//read lines until end of comment marek is found
while (siz != string :: npos)
{
getline(infile, str);
siz = str.find("*/");
comment = false;
}
}
}
}
return 0;
output
Code:
Out.txt
{
ifstream infile("Test.txt");
ofstream outfile("Test2.txt");
string str, search_start="/*", search_end = "*/";
string :: size_type siz;
bool comment = false;
while (getline(infile, str))
{
if (str.find("/*") == string :: npos && str.find("*/") == string :: npos && !comment)
{
outfile << str << endl;
}
else
{
comment = true;
siz = str.find("*/");
if (siz != string :: npos)
{
comment = false;
continue;
}
else
{
while (siz != string :: npos)
{
getline(infile, str);
siz = str.find("*/");
comment = false;
}
}
}
}
return 0;