c++ - Finding and replacing lines from a cpp file -


i trying create program finds multiple lines in file continuously have // , replace them /* * * */. single lines // left is. following example code gives idea of trying perform.

example-original.cpp

#include <iostream> using namespace std;   //to replaced  //to replaced  //to replaced  //to replaced   blah blah blah  ...  ...  int main()  {  ...  ...  //to replaced  //to replaced  }  //to left  return 0;  } 

wantedoutput.cpp

#include <iostream> using namespace std;   /* replaced   *replaced   *replaced   *replaced   */   blah blah blah  ...  ...  int main()  {  ...  ...  /*replaced   *replaced   */  }  //to left  return 0;  } 

i have created program changes first time multiple line comments occur not work other multiple line comments follow them. output stored in "xyz.tmp". file modified provided command line argument. first, have obtained line numbers contains // , stored in array.

from example, array `startcom[]={4,7,16,17}'. line number of first , last lines of multiple line comments stored in array. hope helps understanding code.

then use array check whether contains consecutive values. @ last, read file again , check if matches values in array. tried debugging printing contents of each line before written temp file. shows string has been replaced in output file, doesn't show changes. great if tell why code isn't working 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 " << str << "from position, position " << << "," << << '\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 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 " << << " element "<< 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 line " << j << '\n';                     break;                     }         }     cout << "possible multiple comment lines @ line" << << '\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'; (std::vector<int>::iterator = startcom.begin(); != 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 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 open file" << '\n';  }  }  return 0; } 

what trying consistently format code. consider http://uncrustify.sourceforge.net/, favorite tool.

it has support combining sequential comments.


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -