Wyszukaj i zastąp ciąg w pliku txt w c ++
Chcę znaleźć ciąg w pliku i zastąpić go danymi wprowadzonymi przez użytkownika.
Oto mój szorstki kod.
#include <iostream>
#include <fstream.h>
#include <string.h>
int main(){
istream readFile("test.txt");
string readout,
search,
replace;
while(getline(readFile,readout)){
if(readout == search){
// How do I replace `readout` with `replace`?
}
}
}
AKTUALIZACJA
Oto kod, który rozwiązał mój problem
test.txt:
id_1
arfan
haider
id_2
saleem
haider
id_3
someone
otherone
Kod C ++:
#include <iostream>
#include <fstream>
#include <string>
using namesapce std;
int main(){
istream readFile("test.txt");
string readout,
search,
firstname,
lastname;
cout << "Enter the id which you want to modify";
cin >> search;
while(getline(readFile,readout)){
if(readout == search){
/*
id remains the same
But the First name and Last name are replaced with
the user `firstname` and `lastname` input
*/
cout << "Enter new First name";
cin >> firstname;
cout << "Enter Last name";
cin >> lastname;
}
}
}
Przypuszczać:
Użytkownik wyszukuje identyfikatorid_2
. Po tym użytkowniku wprowadź imię i nazwiskoShafiq
iAhmed
. Po uruchomieniu tego kodutest.txt
Plik musi zmodyfikować rekord w ten sposób:
…
id_2
Shafiq
Ahmad
…
Tylkoid_2
rekord zmienia się, pozostały plik pozostanie taki sam.