c++删除(0,1);在字符串中,删除文件中字符的所有实例
C++ Erase(0,1); in string, removing all instances of character in file
我有一个程序在'isMemLoc()'函数中检查"@",如果它找到一个,它应该删除它。(此符号将始终是该行的第一个字符,因此erase(0,1)调用
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
bool isComment(string line){
string comment = "/";
if(line.find(comment) != string::npos){
return true;
}else{
return false;
}
}
bool isMemLoc(string line){
string symbol = "@";
if(line.find(symbol) != string::npos){
cout << "CONSTANT FOUND" << endl;
//ConvertToBinary(atoi(line.c_str));
return true;
}else{
return false;
}
}
int main( int argc, const char* argv[] )
{
string outLine = "test output";
string file1 = argv[1];
cout << "before: " << file1 << endl;
replace(file1, "asm", "hack");
cout << "after: " << file1 << endl;
//input
//WHILE READ LINE()
ifstream infile(argv[1]);
string tempLine;
ofstream outfile(file1.c_str());
while (getline(infile, tempLine)){
if(isComment(tempLine))
continue;
if(isMemLoc(tempLine)){
tempLine.erase(0);
cout << tempLine << endl;
outfile << tempLine << std::endl;
continue;
}
//print to terminal and pass to file
cout << tempLine << endl;
outfile << tempLine << std::endl;
}
outfile.close();
}
然而,当它找到这个字符时,我的程序也删除了所有找到这个值的行,例如:
1
2
3
13
@12
@12
@13
2
变成
1
2
3
13
2
这是不希望的。我做错了什么?
首先,你的问题中有这个(这是正确的):
tempLine.erase(0, 1);
然后,您将代码更改为(我想这是原始代码):
tempLine.erase(0);
查看参考资料,您会发现count
参数默认为std::string::npos
-擦除字符直到结束
相关文章:
- 正在从C++中的字符数组中删除字符
- 从字符串中删除字符
- 如果不是十六进制,则QT从qlineedit中删除字符
- 从长字符串中删除字符
- 从文件对象的当前位置删除字符
- C++ 删除字符**
- 从字符串中删除字符的第一个和最后一个实例
- 如何在C++中删除字符*数组
- 如何从C 中的字符串中删除字符
- 用“ strcpy”在2D数组中删除(字符)的一行(字符)时是否存在任何错误
- 删除字符并在字符串(C )的末端附加它们
- C++ 字符串,用于删除字符'b'并将字符'a'替换为两个'd'的构建函数
- 在字符串中的定界符之间删除字符
- 从 std::字符串中删除字符从 "(" 到 ")" 用擦除?
- 使用擦除和删除从字符串中删除字符
- C++ 如何删除字符中的双引号
- C++ 中的密码 |打印字符后删除字符
- C++在链表中插入和删除字符
- 字符串流的获取函数不删除字符
- 删除字符C++最后一次出现时字符数组的开头