String in der Datei ersetzen...
-
Hallo,
wie kann ich den ein bestimmtes String in einer txt-Datei durch ein anderes ersetzen???
z.b:
asd sdf jlk
(sdf durch qwe)Ergebnis:
asd qwe jlk
-
Datei einlesen, String finden, String ersetzen, geänderten String in Datei schreiben.
-
Also doch die komplette Datei einlesen...
-
hi!
habs mal versucht schnell zu coden;-) jedoch schreibt er bei mir nichts in die datei...dh.: er überschreibt die alte datei und die bleibt dann leer...hmm weiss jetzt auch nicht warum...?cu
#include <algorithm> #include <fstream> #include <iostream> #include <string> #include <vector> #include <map> using namespace std; #define STR(x) # x #define SSTR(x) STR(x) class LogBook { private: std::string filename; std::string search_str; std::string replace_str; std::string txt_str; fstream m_stream; void openfile(bool read_write) { try { int pos = filename.find("."); string s1 = filename.substr(pos, filename.length()); if(s1 != ".txt") throw runtime_error("::File open failed, unsupported file format! " __FILE__ " " SSTR(__LINE__)); // Datei oeffnen if(read_write == 0) { m_stream.open(filename.c_str(), ios::in); } else { m_stream.open(filename.c_str(), ios::out); } if (!m_stream) { throw runtime_error("::File open failed! " __FILE__ " " SSTR(__LINE__)); } } catch(...) { m_stream.close(); throw; } } void replace_all(string &s, const char *from, const char *to) { unsigned pos = 0, len = strlen( from ); while ((pos = s.find( from, pos )) != s.npos) { s.replace( pos, len, to ); } } void read() { std::string str_read; while (m_stream.good()) { str_read = ""; getline(m_stream, str_read); replace_all(str_read, search_str.c_str(), replace_str.c_str()); } txt_str += str_read; } void write() { m_stream << txt_str << endl; cout << txt_str << endl; } void closefile() { m_stream.close(); } public: LogBook(const std::string &search_str_, const std::string &replace_str_, const std::string &filename_): search_str(search_str_), replace_str(replace_str_), filename(filename_) {} void replace() { openfile(false); read(); closefile(); openfile(true); write(); closefile(); } }; template<class T> void WaitForReturn(T& stream) { stream.clear(); stream.ignore(stream.rdbuf()->in_avail()); stream.get(); } int main() { LogBook book("hallo", "sers", "File.txt"); book.replace(); WaitForReturn(cin); return 0; }
-
hi!
so nun fehler gefunden, problem war: ich hatte den fstream nicht gelöscht vor dem öffnen...m_stream.clear();
LogBook book("hallo", "sers", "File.txt");
in der datei File.txt werden alle hallo durch sers ersetzt...
code ist sicher nicht der schöneste...soll nur mal zeigen wie es gehen kann;-)cu
#include <fstream> #include <iostream> #include <string> using namespace std; #define STR(x) # x #define SSTR(x) STR(x) class LogBook { private: std::string filename; std::string search_str; std::string replace_str; std::string txt_str; fstream m_stream; void openfile(bool read_write) { try { int pos = filename.find("."); string s1 = filename.substr(pos, filename.length()); if(s1 != ".txt") throw runtime_error("::File open failed, unsupported file format! " __FILE__ " " SSTR(__LINE__)); m_stream.clear(); // Datei oeffnen if(read_write == 0) { m_stream.open(filename.c_str(), ios::in); } else { m_stream.open(filename.c_str(), ios::out); } if (!m_stream) { throw runtime_error("::File open failed! " __FILE__ " " SSTR(__LINE__)); } } catch(...) { m_stream.close(); throw; } } void replace_all(std::string &s, const char *from, const char *to) { unsigned pos = 0, len = strlen( from ); while ((pos = s.find( from, pos )) != s.npos) { s.replace( pos, len, to ); } } void read() { std::string str_read; while (m_stream.good()) { str_read = ""; getline(m_stream, str_read); replace_all(str_read, search_str.c_str(), replace_str.c_str()); txt_str += str_read; txt_str += '\n'; } if(!txt_str.empty() && txt_str[txt_str.length()-1]=='\n') { txt_str.erase(txt_str.end()-1); } } void write() { m_stream << txt_str; cout << txt_str << endl; } void closefile() { m_stream.close(); } public: LogBook(const std::string &search_str_, const std::string &replace_str_, const std::string &filename_): search_str(search_str_), replace_str(replace_str_), filename(filename_) {} void replace() { openfile(false); read(); closefile(); openfile(true); write(); closefile(); } }; template<class T> void WaitForReturn(T& stream) { stream.clear(); stream.ignore(stream.rdbuf()->in_avail()); stream.get(); } int main() { LogBook book("hallo", "sers", "File.txt"); book.replace(); WaitForReturn(cin); return 0; }