首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从cpp文件中查找和替换行

从cpp文件中查找和替换行
EN

Stack Overflow用户
提问于 2014-12-01 22:37:00
回答 1查看 81关注 0票数 0

我正在尝试创建一个程序,它可以在一个文件中找到连续拥有//的多行代码,并将它们替换为/* * * */。但是带有//的单行保持原样。下面的示例代码很好地了解了我正在尝试执行的操作。

Example-original.cpp

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#include <iostream>
using namespace std;

 /* replaced
  *replaced
  *replaced
  *replaced
  */

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

我已经创建了一个程序,它成功地改变了第一次发生多行注释,但它不适用于其他多行注释。输出存储在"xyz.tmp“中。要修改的文件作为命令行参数提供。首先,我获得了包含//的行号,并将其存储在一个数组中。

从这个例子中,我的数组将是‘startcom[]={4,7,16,17}’。多行注释的第一行和最后一行的行号存储在此数组中。我希望这有助于理解代码。

然后我使用这个数组来检查它是否包含连续的值。最后,我再次读取该文件,并检查它是否与该数组中的值匹配。在将每一行内容写入临时文件之前,我尝试通过打印它的内容进行调试。它显示字符串已被替换,但在输出文件中没有显示任何更改。如果有人能知道为什么代码不适用于多个注释行的其他实例,那就太好了。

mycode.cpp

代码语言:javascript
运行
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-01 22:56:11

您要做的是一致地格式化代码。以http://uncrustify.sourceforge.net/为例,它是我最喜欢的工具。

它特别支持合并顺序注释。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27238660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档