Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- int main(int argc , char* argv[])
- {
- char* fileName = argv[1]; //przypisanie argumentu do tablicy char
- string newFileName = fileName;
- newFileName = newFileName.replace(newFileName.find("."), newFileName.find(".") + 3, "_nowe.txt"); // zastąpienie "." "_nowe.txt" po numerze pozycji
- string txtOld = argv[2];
- string txtNew = argv[3];
- fstream file;
- fstream newfile;
- string line;
- string replacingTxt;
- file.open( fileName, ios::in ); //otwieranie pliku
- if(file.good() == true){ //spradzenie czy plik sie otworzyl prawidlowo
- cout << "Glowny plik istnieje" << endl;
- newfile.open(newFileName.c_str(), ios::out); //otwieranie/tworzenie nowego pliku
- if(newfile.good() == true){
- while (getline(file, line))
- {
- int x = line.find(txtOld);
- if (x != -1)
- {
- replacingTxt += line.replace(x, txtOld.length(), txtNew) + "\n"; // wrzuca do replacingTxt tekst ze starego pliku automatycznie zamieniajac na nowe wartosci
- }
- else
- {
- replacingTxt += line + "\n"; //nowa linia
- }
- }
- newfile << replacingTxt; // zapis tekstu do pliku
- cout << "Gotowe" << endl;
- } else {
- cout << "Nastąpil problem przy tworzeniu pliku" << endl;
- file.close(); //zamykanie pliku
- return 1;
- }
- } else {
- cout << "Glowny plik nie istnieje" << endl;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement