Ravi
Ravi

Reputation: 477

Finding and replacing lines from a cpp file

I am trying to create a program which finds multiple lines in a file which continuously have // and replace them with /* * * */. But single lines with // are left as it is. The following example code gives a good idea of what I am trying to perform.

Example-original.cpp

#include <iostream>
using namespace std;

 //to be replaced
 //to be replaced
 //to be replaced
 //to be replaced

 BLAH BLAH BLAH
 ...
 ...
 int main()
 {
 ...
 ...
 //to be replaced
 //to be replaced
 }
 //to be left as it is
 return 0;
 }

Wantedoutput.cpp

#include <iostream>
using namespace std;

 /* replaced
  *replaced
  *replaced
  *replaced
  */

 BLAH BLAH BLAH
 ...
 ...
 int main()
 {
 ...
 ...
 /*replaced
  *replaced
  */
 }
 //to be left as it is
 return 0;
 }

I have created a program which successfully changes the first time multiple line comments occur but it does not work for other multiple line comments which follow them. The output is stored in "xyz.tmp". The file to be modified is provided as a command line argument. First, I have obtained the line numbers which contains // and stored it in an array.

From the example, my array will be `startcom[]={4,7,16,17}'. The line number of first and last lines of multiple line comments is stored in this array. I hope this helps understanding the code.

Then I use this array to check whether it contains consecutive values. At last, I read the file again and check if it matches with the values in that array. I tried debugging by printing the contents of each line before it is written to the temp file. It shows that the string has been replaced but in the output file, it doesn't show any changes. It would be great if anyone could tell why the code isn't working for other instances of multiple comment lines.

mycode.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include <sys/stat.h>
using namespace std;

bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
//cout << "within function " << "string is " << str << "from position, to position " << from << "," << to << '\n';
if(start_pos == std::string::npos)
    return false;
str.replace(start_pos, from.length(), to);
return true;
}



int main (int argc, char* argv[])
{
string line;
int linecount=0, linearray[1000],temp=0;
std::vector<int> startcom, endcom; 
ofstream TempFile ("xyz.tmp");

ifstream myfile (argv[1]);
if (myfile.is_open())
{
    while ( getline (myfile,line) )
    {
        linecount++;
        if (line.find("//") != std::string::npos)
        {   
            linearray[temp]=linecount;
            temp++;
            cout << "linecount->" <<linecount << "comment line" << line << '\n';
        }
    }
myfile.close();
}
else
{
    cout << "Unable to open file"; 
}
for(int k=0;k<temp;k++)
cout << "array elements are: " << linearray[k] << '\n';
cout << "size of temp is: " <<temp << '\n';
for(int i=0;i<temp;i++)
{
    int j=0;
    cout << "for loop " << i << " element is "<< linearray[i] <<'\n';
    if((linearray[i]+1) == linearray[i+1])
    {
        startcom.push_back(i);
        cout << "outer if part" << '\n';
        for(j=i+1; j < 10; j++)
        {
                    if((linearray[j]+1) == linearray[j+1])
                    {
                        cout << "still continuing" << "j value " << j << '\n';
                    }
                    else
                    {
                        startcom.push_back(j);
                        i=j;
                        cout << " inner else part for line " << j << '\n';
                    break;
                    }
        }
    cout << "possible multiple comment lines at line" << i << '\n';
    }
    else
    {
        cout << "outer else part" << '\n';
    }
cout << "array element " << linearray[i] << '\n';
}
//for listing out elements of startcom,endcom arrays
cout << "startcom value " << '\n';
for (std::vector<int>::iterator it = startcom.begin(); it != startcom.end(); ++it)
std::cout << ' ' << *it;
cout << "startcom size is" << startcom.size() << "\n";
linecount=0;
int tmpcount=0,a=0,b=0;
ifstream myfile1 (argv[1]);
for(tmpcount=0;tmpcount<startcom.size();tmpcount++)
{
if (myfile1.is_open())
{
    while ( getline (myfile1,line) )
    {
        linecount++;
        a=startcom.at(tmpcount);
        b=startcom.at(tmpcount+1);
        if (linecount == linearray[a] && a!= b)
        {   
            cout << "before replacing (startcom)  ---> " << "at line -->" << linecount << line << '\n';

            replace(line, "//", "/*");
            TempFile << line << '\n';
            //while(replace(line, "//", "/*"))
            //{
            //  cout << " success repace " << '\n';
            //}
            //linearray[temp]=linecount;
            //temp++;
            //cout << "linecount->" <<linecount << "this line contains //" << line << '\n';
            cout << "this line has been replaced ---> " << line << '\n';
        }

        else if (linecount == linearray[b] && a!= b)
        {   
            cout << "before replacing (endcom) ---> "  << "at line -->" << linecount << line << '\n';
            replace(line, "//", " *");
            TempFile << line << '\n';
            TempFile << "*/" << '\n';
            cout << "this line has been replaced ---> " << line << '\n';
        }
        else if (linecount > linearray[a] && linecount < linearray[b] && a!= b)
        {
            cout << "before replacing (start to end) ---> "  << "at line -->" << linecount << line << '\n';
            replace(line, "//", " *");
            TempFile << line << '\n';
            cout << "this line has been replaced ---> " << line << '\n';
        }
        else
        {
            cout << "No replacement " << "at line -->" << linecount << "\n" ;
            TempFile << line << '\n';
        }
    }
myfile1.close();
TempFile.close();
}
else
{
    cout << "Unable to open file" << '\n'; 
}

}

return 0;
}

Upvotes: 0

Views: 86

Answers (1)

Sanjaya R
Sanjaya R

Reputation: 6426

What you are trying to do is consistently format your code. Consider http://uncrustify.sourceforge.net/, its my favorite tool.

It specifically has support for combining sequential comments.

Upvotes: 1

Related Questions