visual studio 2012-在C++中删除注释的程序不起作用
visual studio 2012 - Program for removing comments in C++ not working
我用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;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 为什么我的程序在删除以下代码中的注释后会出现运行时错误?
- 每当我运行注释代码时,程序就会崩溃
- 为什么添加内联程序集注释会导致 GCC 生成的代码发生如此根本的变化?
- 编译三个文件以创建一个用于平均值的程序(取消注释?)
- 生成注释以解释C++程序的结构
- 仅将 c++ 程序中的注释转换为大写
- 在 C 中的程序开发期间删除注释
- 在一个简单的c++ 11四线程程序中,如果我注释标准输出打印行,我的两个消费者线程就不会返回
- visual studio 2012-在C++中删除注释的程序不起作用