C++ Erase(0,1); in string, removing all instances of character in file -
i have program checks "@" in 'ismemloc()' function, , if finds 1 should remove it. (this symbol first character on line, hence erase(0,1) call
#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 terminal , pass file cout << templine << endl; outfile << templine << std::endl; } outfile.close(); } however, when finds character, program deleting of lines value found eg:
1 2 3 13 @12 @12 @13 2 turns into
1 2 3 13 2 this undesired. doing wrong?
first, had in question (which right):
templine.erase(0, 1); then, changed code (i suppose it's original one):
templine.erase(0); see reference , you'll find out count parameter defaulted std::string::npos - erases characters till end.
Comments
Post a Comment