Advertisement
Guest User

Brzesko

a guest
Nov 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. int main()
  6. {
  7.  
  8. string filename,data,c1,c2,line;
  9.  
  10. cin>>filename;
  11. cin>>c1;
  12. cin>>c2;
  13.  
  14.  
  15. string nfilename=filename;
  16. int txtPosition=nfilename.find(".txt");
  17. nfilename.replace(txtPosition,txtPosition+4,"_nowy.txt");
  18.  
  19.  
  20. ifstream file(filename.c_str());;
  21. if(!file.good())
  22. return -1;
  23.  
  24. // Wczytanie tekstu z pliku do zmiennej data
  25. while ( getline (file,line) ){
  26. data=line;
  27. }
  28.  
  29. file.close();
  30.  
  31. //Zamiana ciagu c1 na c2
  32. int occurence=data.find(c1);
  33. while(occurence!=string::npos){
  34. data.erase(occurence,c1.size());
  35. data.insert(occurence,c2);
  36. occurence=data.find(c1,occurence+1);
  37. }
  38.  
  39. //Utworzenie nowego pliku
  40. ofstream newfile(nfilename.c_str());
  41. newfile<<data;
  42. newfile.close();
  43.  
  44.  
  45.  
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement