35657

Untitled

Dec 1st, 2023
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 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.         while (!fin.eof()) {
  24.             getline(fin, str); // считываем последовательность символов до перевода строки
  25.             if (str != "") {
  26.                 fout << str << endl;
  27.             }
  28.         }
  29.         fin.close();
  30.         fout.close();
  31.         remove("C:/Users/PC/Desktop/file.txt"); // удаляем старый файл
  32.         rename("C:/Users/PC/Desktop/file2.txt", "C:/Users/PC/Desktop/file.txt"); // переименовываем новый файл (называем его именем старого)
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment