Advertisement
Voldemord

zadanie4

Nov 21st, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc , char* argv[])
  8. {
  9.     char* fileName = argv[1]; //przypisanie argumentu do tablicy char
  10.     string newFileName = fileName;
  11.  
  12.     newFileName = newFileName.replace(newFileName.find("."), newFileName.find(".") + 3, "_nowe.txt"); // zastąpienie "." "_nowe.txt" po numerze pozycji
  13.  
  14.     string txtOld = argv[2];
  15.     string txtNew = argv[3];
  16.  
  17.     fstream file;
  18.     fstream newfile;
  19.  
  20.     string line;
  21.     string replacingTxt;
  22.  
  23.     file.open( fileName, ios::in ); //otwieranie pliku
  24.  
  25.     if(file.good() == true){ //spradzenie czy plik sie otworzyl prawidlowo
  26.         cout << "Glowny plik istnieje" << endl;
  27.  
  28.         newfile.open(newFileName.c_str(), ios::out); //otwieranie/tworzenie nowego pliku
  29.  
  30.         if(newfile.good() == true){
  31.  
  32.                 while (getline(file, line))
  33.                 {
  34.                     int x = line.find(txtOld);
  35.  
  36.                     if (x != -1)
  37.                     {
  38.  
  39.                         replacingTxt += line.replace(x, txtOld.length(), txtNew) + "\n"; // wrzuca do replacingTxt tekst ze starego pliku automatycznie zamieniajac na nowe wartosci
  40.  
  41.                     }
  42.                     else
  43.                     {
  44.  
  45.                         replacingTxt += line + "\n"; //nowa linia
  46.  
  47.                     }
  48.                 }
  49.                 newfile << replacingTxt; // zapis tekstu do pliku
  50.                 cout << "Gotowe" << endl;
  51.  
  52.         } else {
  53.             cout << "Nastąpil problem przy tworzeniu pliku" << endl;
  54.             file.close(); //zamykanie pliku
  55.             return 1;
  56.  
  57.         }
  58.  
  59.     } else {
  60.         cout << "Glowny plik nie istnieje" << endl;
  61.         return 1;
  62.     }
  63.  
  64.  
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement