Advertisement
kxcoze

satie_new_lab

Oct 16th, 2020 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string>
  3. #include <iostream>
  4. #include <Windows.h>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. short menu();
  10. void deleteStr(int);
  11. void listNumeration(int);
  12. void inputInFile();
  13. void list(int);
  14. void clearFile();
  15. int countLines();
  16.  
  17.  
  18. int main() {
  19.     SetConsoleCP(1251);
  20.     SetConsoleOutputCP(1251);
  21.     short choise;
  22.     do {
  23.         int countOfLine = countLines();
  24.         choise = menu();
  25.         switch (choise) {
  26.         case 1:
  27.             system("cls");
  28.             inputInFile();
  29.             system("pause");
  30.             system("cls");
  31.             break;
  32.         case 2:
  33.             system("cls");
  34.             list(countOfLine);
  35.             system("pause");
  36.             system("cls");
  37.             break;
  38.         case 3:
  39.             system("cls");
  40.             listNumeration(countOfLine);
  41.             system("pause");
  42.             system("cls");
  43.             break;
  44.         case 4:
  45.             system("cls");
  46.             deleteStr(countOfLine);
  47.             system("pause");
  48.             system("cls");
  49.             break;
  50.         case 5:
  51.             system("cls");
  52.             clearFile();
  53.             system("pause");
  54.             system("cls");
  55.             break;
  56.         case 0:
  57.             cout << "До свидания!!!" << endl;
  58.             break;
  59.         default:
  60.             cout << "Неверный выбор!!!" << endl;
  61.             cout << "\n\nPress Enter..." << endl;
  62.             system("cls");
  63.             break;
  64.         }
  65.     } while (choise);
  66.     return 0;
  67. }  
  68.  
  69. short menu() {
  70.     short v;
  71.     cout << "           МЕНЮ" << endl;
  72.     cout << " Введите [1] для ввода текста\n";
  73.     cout << " Введите [2] для просмотра текста\n";
  74.     cout << " Введите [3] для вывода текста с нумерацией строк\n";
  75.     cout << " Введите [4] для удаления i-ой строки из текста\n";
  76.     cout << " Введите [5] для очистки файла\n";
  77.     cout << " Введите [0] для выхода\n";
  78.     cout << " Пункт: ";
  79.    
  80.     cin >> v;
  81.  
  82.     return v;
  83. }
  84.  
  85.  
  86. void deleteStr(int countOfLine) {
  87.     listNumeration(countOfLine);
  88.  
  89.     int d, i = 0;
  90.     string s;
  91.     cout << "Введите номер строки, которую вы хотите удалить: ";
  92.     cin >> d;
  93.  
  94.     if (d >= countOfLine) {
  95.         cout << "Такого номера строки не существует!\n";
  96.         return;
  97.     }
  98.  
  99.     ifstream fin("data.txt");
  100.     ofstream fout("changed_data.txt" , ios_base::trunc);
  101.     while (!fin.eof() && i < countOfLine-1)
  102.     {
  103.         getline(fin, s);
  104.         if (++i != d)
  105.             fout << s + '\n';
  106.     }
  107.     fin.close();
  108.     fout.close();
  109.  
  110.     remove("data.txt");
  111.     rename("changed_data.txt", "data.txt");
  112. }
  113.  
  114. void list(int countOfLine) {
  115.     cout << "Текст из файла: \n";
  116.     string s;
  117.     int i = 0;
  118.     ifstream fin("data.txt"); //Открыли файл для чтения
  119.     while (!fin.eof() && ++i < countOfLine) //Будем читать информацию пока не дойдем до конца файла
  120.     {
  121.         getline(fin, s);
  122.         cout << s << endl;
  123.     }
  124.     fin.close();
  125. }
  126.  
  127. void listNumeration(int countOfLine) {
  128.     cout << "Текст из файла: \n";
  129.     string s;
  130.     int i = 0;
  131.     ifstream fin("data.txt");
  132.     while (!fin.eof() && ++i < countOfLine)
  133.     {
  134.         getline(fin, s);
  135.         cout << i << ": " << s << endl;
  136.     }
  137.     fin.close();
  138. }
  139.  
  140. /*Ввод текста в файл*/
  141. void inputInFile()
  142. {
  143.     cout << "Ввод в файл:  \n";
  144.     string s;
  145.     bool flag = true;
  146.     ofstream fout("data.txt", ios_base::out | ios_base::app);
  147.    
  148.     cout << "Когда закончите, введите +: " << endl;
  149.     cin.ignore();
  150.     while (getline(cin, s)) {
  151.         if (s[0] == '+' && s.size() == 1) {
  152.             break;
  153.         }
  154.         fout << s << endl;
  155.     }
  156.     fout.close();
  157. }
  158.  
  159. void clearFile() {
  160.     cout << "Файл успешно очищен! \n";
  161.     ofstream fclear("data.txt", ios_base::trunc);
  162.     fclear.close();
  163. }
  164.  
  165. int countLines() {
  166.     string s;
  167.     int cnt = 0;
  168.     ifstream fin("data.txt");
  169.     while (!fin.eof())
  170.     {  
  171.         getline(fin, s);
  172.         cnt++;
  173.     }
  174.     return cnt;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement