visual studio 2012-在C++中删除注释的程序不起作用

visual studio 2012 - Program for removing comments in C++ not working

本文关键字:注释 程序 不起作用 删除 studio 2012- C++ visual      更新时间:2023-10-16

我用c++创建了一个程序,删除了一个c/c++文件的命令,并将一个注释删除的版本放在另一个文件中。然而,经过数小时的调试,它仍然无法工作。救命!

"input"是一个字符串,它具有c/c++文件的文件夹位置。"files"是一个向量,包含文件夹中的所有文件名,但不包含它们的位置。我使用"输入"answers"文件"来获取文件名和位置。

for (unsigned int i = 0; i < files.size();i++ ){//for loop start
iteratora++;
string filename1 = input;
filename1.append("\");
filename1.append(files[iteratora + 2]);
    cout << "n" << filename1 << ".n";
    cout << "Iterator: " << iteratora << ".n";
programFile.clear();
ifstream afile (filename1);//(filename1);
fstream temp ("temp/temp.txt",std::ofstream::out | std::ofstream::trunc);
string line;//variable for holding the characters in one line
remove_comments(afile,temp);

if (temp.is_open())
{
    while ( getline (temp,line) )
    {
        //cout << line << 'n';
        if (line != ""){
            cout << line;
        programFile.push_back(line);
        line = "";
        }
    }
    temp.close();
}
temp.clear();

 if (showVerbose == true){
       print_vector(programFile);//used to know what is in the file
   }

}

删除注释功能

void remove_comments ( ifstream& Source , fstream& 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 ( flag )
{   if ( line.find("*/") < line.length() )
flag = false;
line.erase(0,line.find("*/") + 2);
}

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;

}

}
Source.close(); // to close the opened files.
Target.close(); 
}

谢谢!

字符串的find方法在搜索不成功时返回std::string::npos,所以应该写这行

if(line.find("/*") < line.length())

如下所示:

if(line.find("/*") != std::string::npos)

进行类似的更改并尝试。

这里是另一个解决方案。它打开file.txt并将整个文件加载到string中。然后它删除评论并将CCD_ 3中的新数据写回到CCD_。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void) {
        string source;
        ifstream readFile("file.txt");
        getline(readFile, source, '');    
        readFile.close();
        cout<<"n---b e f o r e -----------------------------------nn";
         cout << source;
        while(source.find("/*") != string::npos) {
            size_t Beg = source.find("/*");
            source.erase(Beg, (source.find("*/", Beg) - Beg)+2);
        }
        while(source.find("//") != string::npos) {
            size_t Beg = source.find("//");
            source.erase(Beg, source.find("n", Beg) - Beg);
        }   
       ofstream writefile("file.txt");
       writefile <<source;
       writefile.close();

       cout<<"nn-- a f t e r -------------------------------------------nn";

        cout << source;
        cout<<"n---------------------------------------------nn";
        cout<<"nn";
    return 0;
}