#include <fstream> <!--more--> #include <iostream> #include <string> #include "ichar_traits.h" using namespace std;
istring getData(string filename) throw(std::ifstream::failure) { std::ifstream streamReader(filename, std::ios::binary); if (!streamReader.is_open()) throw std::ifstream::failure("file couldn't open"); streamReader.seekg(0, std::ios::end); unsigned filesize = streamReader.tellg(); char *_data = new char[filesize]; streamReader.seekg(0, std::ios::beg); streamReader.read(_data, filesize); streamReader.close(); istring data(_data); delete[] _data; return std::move(data); }
istring &replaceAll(istring &context, const istring &from, const istring &to) { size_t lookHere = 0; size_t foundHere; while ((foundHere = context.find(from, lookHere)) != istring::npos) { context.replace(foundHere, from.size(), to); lookHere = foundHere + to.size(); #ifdef SHOWREPLACE cout << "stringNumber:" << foundHere << endl; #endif } return context; }
int main(int argc, char const *argv[]) { istring fileTxt = getData("Rparse.h"); cout<<fileTxt<<endl; replaceAll(fileTxt,"Size","Hello"); cout<<fileTxt<<endl; return 0; }
|