35657

Untitled

Dec 1st, 2023
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <Windows.h>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     SetConsoleCP(1251);
  10.     SetConsoleOutputCP(1251);
  11.  
  12.     ifstream fin;
  13.     fin.open("C:/Users/PC/Desktop/file.txt");
  14.  
  15.     ofstream fout;
  16.     fout.open("C:/Users/PC/Desktop/file2.txt");
  17.  
  18.     if (!fin.is_open() || !fout.is_open()) {
  19.         cout << "Ошибка открытия файла" << endl;
  20.     }
  21.     else {
  22.         string str;
  23.  
  24.         getline(fin, str);
  25.         if (!fin.eof()) {
  26.             fout << str;
  27.         }
  28.  
  29.         while (!fin.eof()) {
  30.             getline(fin, str);
  31.             if (!fin.eof()) {
  32.                 fout << endl << str;
  33.             }
  34.         }
  35.         fin.close();
  36.         fout.close();
  37.         remove("C:/Users/PC/Desktop/file.txt"); // удаляем старый файл
  38.         rename("C:/Users/PC/Desktop/file2.txt", "C:/Users/PC/Desktop/file.txt"); // переименовываем новый файл (называем его именем старого)
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment