Advertisement
kxcoze

satie-1700 LTE

Oct 29th, 2020 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.64 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. bool checkEmptiness();
  17.  
  18. int main() {
  19.     SetConsoleCP(1251);
  20.     SetConsoleOutputCP(1251);
  21.     if (!checkEmptiness()) return -1;
  22.    
  23.     short choise;
  24.     do {
  25.         int countOfLine = countLines();
  26.         choise = menu();
  27.         switch (choise) {
  28.         case 1:
  29.             system("cls");
  30.             inputInFile();
  31.             system("pause");
  32.             system("cls");
  33.             break;
  34.         case 2:
  35.             system("cls");
  36.             list(countOfLine);
  37.             system("pause");
  38.             system("cls");
  39.             break;
  40.         case 3:
  41.             system("cls");
  42.             listNumeration(countOfLine);
  43.             system("pause");
  44.             system("cls");
  45.             break;
  46.         case 4:
  47.             system("cls");
  48.             deleteStr(countOfLine);
  49.             system("pause");
  50.             system("cls");
  51.             break;
  52.         case 5:
  53.             system("cls");
  54.             clearFile();
  55.             system("pause");
  56.             system("cls");
  57.             break;
  58.         case 0:
  59.             cout << "До свидания!!!" << endl;
  60.             break;
  61.         default:
  62.             cout << "Неверный выбор!!!" << endl;
  63.             cout << "\n\nPress Enter..." << endl;
  64.             system("cls");
  65.             break;
  66.         }
  67.     } while (choise);
  68.     return 0;
  69. }
  70.  
  71. short menu() {
  72.     short v;
  73.     cout << "             МЕНЮ" << endl;
  74.     cout << " Введите [1] для ввода текста\n";
  75.     cout << " Введите [2] для просмотра текста\n";
  76.     cout << " Введите [3] для вывода текста с нумерацией строк\n";
  77.     cout << " Введите [4] для удаления i-ой строки из текста\n";
  78.     cout << " Введите [5] для очистки файла\n";
  79.     cout << " Введите [0] для выхода\n";
  80.     cout << " Пункт: ";
  81.  
  82.     cin >> v;
  83.  
  84.     return v;
  85. }
  86.  
  87.  
  88. void deleteStr(int countOfLine) {  //удаление i-ой строки
  89.     if (!checkEmptiness()) return;
  90.     listNumeration(countOfLine);
  91.     int d, i = 0;
  92.     string s;
  93.     cout << "\nВведите номер строки, которую вы хотите удалить: ";
  94.     cin >> d;
  95.  
  96.     if (d >= countOfLine) {
  97.         cout << "Такого номера строки не существует!\n";
  98.         return;
  99.     }
  100.  
  101.     ifstream fin("data.txt");
  102.     ofstream fout("changed_data.txt", ios_base::trunc); //очистка содержимого файла
  103.     while (!fin.eof() && i < countOfLine - 1)
  104.     {
  105.         getline(fin, s);
  106.         if (++i != d)
  107.             fout << s + '\n';
  108.     }
  109.     fin.close();
  110.     fout.close();
  111.  
  112.     remove("data.txt"); //удаление файла
  113.     rename("changed_data.txt", "data.txt");  //переиминовывает 1 файл на второй
  114. }
  115.  
  116. void list(int countOfLine) { //вывод строк из файла на экран
  117.     if (!checkEmptiness()) return;
  118.  
  119.     int cut, t = 0;
  120.     cout << "Введите значение до которого будет выводится строка\n";
  121.     cin >> cut;
  122.     cut = cut;
  123.     ifstream fin("data.txt");
  124.      // если первый символ конец файла
  125.    
  126.     cout << "Текст из файла: \n";
  127.     string s;
  128.     int i = 0;
  129.     while (!fin.eof() && ++i < countOfLine) //Будем читать информацию, пока не дойдем до конца файла
  130.     {
  131.         int u = 0, k = t;
  132.         getline(fin, s);
  133.         while (cut > 0) {
  134.             if (u >= s.size()) {
  135.                 t = k;
  136.                 break;
  137.             }
  138.             else if (k >= cut) {
  139.                 cout << endl;
  140.                 k = 0;
  141.             }
  142.             cout << s[u++];
  143.             k++;
  144.         }
  145.     }
  146.     cout << endl;
  147.     fin.close();
  148. }
  149.  
  150. void listNumeration(int countOfLine) { //вывод строк с нумерацией
  151.     if (!checkEmptiness()) return;
  152.  
  153.     cout << "Текст из файла: \n";
  154.     string s;
  155.     int i = 0;
  156.     ifstream fin("data.txt");
  157.     while (!fin.eof() && ++i < countOfLine)
  158.     {
  159.         getline(fin, s);
  160.         cout << i << ": " << s << endl;
  161.     }
  162.     fin.close();
  163. }
  164.  
  165.  
  166. void inputInFile() { //Ввод текста в файл
  167.     if (!checkEmptiness()) return;
  168.     cout << "Ввод в файл:  \n";
  169.     string s;
  170.     bool flag = true;
  171.     ofstream fout("data.txt", ios_base::out | ios_base::app);
  172.     cout << "Когда закончите, введите +: " << endl;
  173.     cin.ignore();
  174.     while (getline(cin, s)) {
  175.         if (s[0] == '+' && s.size() == 1) {
  176.             break;
  177.         }
  178.         fout << s << endl;
  179.     }
  180.     fout.close();
  181. }
  182.  
  183. /*void out() {
  184.     system("CLS");
  185.     int d;
  186.     ifstream fin("data.txt");
  187.     if (!fin) {        //Если файл не существует, то
  188.         cout << "А где текст? Так не пойдёт, сначала введите его, а уж потом я разделю на строчки!";
  189.     }
  190.     else {
  191.         cout << "Подскажите длинну строки: ";
  192.         cin >> d;
  193.         string text;
  194.         int c = 0;
  195.         int i = 0;
  196.         getline(fin, s);
  197.         while (c < text.length()) {
  198.             string TX = text.substr(c, d);
  199.             c += d;
  200.             X[i] = new string;
  201.             *X[i] = TX;
  202.             i++;
  203.         }
  204.         for (int j = 0; j < i; j++) cout << *X[j] << endl;
  205.         cr = false;
  206.     }
  207.     fin.close();
  208. }*/
  209.  
  210. void clearFile() {  //очистка файла
  211.     cout << "Файл успешно очищен! \n";
  212.     ofstream fclear("data.txt", ios_base::trunc);
  213.  
  214.     fclear.close();
  215. }
  216.  
  217. int countLines() {  //считаем количество строк
  218.     if (!checkEmptiness()) return -1;
  219.  
  220.     string s;
  221.     int cnt = 0;
  222.     ifstream fin("data.txt");
  223.     while (!fin.eof())
  224.     {
  225.         getline(fin, s);
  226.         cnt++;
  227.     }
  228.     return cnt;
  229. }
  230.  
  231. bool checkEmptiness() {
  232.     bool flag = true;
  233.     ifstream file("data.txt");
  234.     if (file.is_open()) {
  235.         if (file.peek() == EOF) {
  236.             cout << "Ваш файл пуст! Введите текст в файл.\n";
  237.             flag = false;
  238.         }
  239.         file.close();
  240.     }
  241.     else {
  242.         cout << "Файл не существует.\n";
  243.         flag = false;
  244.     }
  245.     return flag;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement