Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include <iostream>
- #include <Windows.h>
- #include <fstream>
- using namespace std;
- short menu();
- void deleteStr(int);
- void listNumeration(int);
- void inputInFile();
- void list(int);
- void clearFile();
- int countLines();
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- short choise;
- do {
- int countOfLine = countLines();
- choise = menu();
- switch (choise) {
- case 1:
- system("cls");
- inputInFile();
- system("pause");
- system("cls");
- break;
- case 2:
- system("cls");
- list(countOfLine);
- system("pause");
- system("cls");
- break;
- case 3:
- system("cls");
- listNumeration(countOfLine);
- system("pause");
- system("cls");
- break;
- case 4:
- system("cls");
- deleteStr(countOfLine);
- system("pause");
- system("cls");
- break;
- case 5:
- system("cls");
- clearFile();
- system("pause");
- system("cls");
- break;
- case 0:
- cout << "До свидания!!!" << endl;
- break;
- default:
- cout << "Неверный выбор!!!" << endl;
- cout << "\n\nPress Enter..." << endl;
- system("cls");
- break;
- }
- } while (choise);
- return 0;
- }
- short menu() {
- short v;
- cout << " МЕНЮ" << endl;
- cout << " Введите [1] для ввода текста\n";
- cout << " Введите [2] для просмотра текста\n";
- cout << " Введите [3] для вывода текста с нумерацией строк\n";
- cout << " Введите [4] для удаления i-ой строки из текста\n";
- cout << " Введите [5] для очистки файла\n";
- cout << " Введите [0] для выхода\n";
- cout << " Пункт: ";
- cin >> v;
- return v;
- }
- void deleteStr(int countOfLine) {
- listNumeration(countOfLine);
- int d, i = 0;
- string s;
- cout << "Введите номер строки, которую вы хотите удалить: ";
- cin >> d;
- if (d >= countOfLine) {
- cout << "Такого номера строки не существует!\n";
- return;
- }
- ifstream fin("data.txt");
- ofstream fout("changed_data.txt" , ios_base::trunc);
- while (!fin.eof() && i < countOfLine-1)
- {
- getline(fin, s);
- if (++i != d)
- fout << s + '\n';
- }
- fin.close();
- fout.close();
- remove("data.txt");
- rename("changed_data.txt", "data.txt");
- }
- void list(int countOfLine) {
- cout << "Текст из файла: \n";
- string s;
- int i = 0;
- ifstream fin("data.txt"); //Открыли файл для чтения
- while (!fin.eof() && ++i < countOfLine) //Будем читать информацию пока не дойдем до конца файла
- {
- getline(fin, s);
- cout << s << endl;
- }
- fin.close();
- }
- void listNumeration(int countOfLine) {
- cout << "Текст из файла: \n";
- string s;
- int i = 0;
- ifstream fin("data.txt");
- while (!fin.eof() && ++i < countOfLine)
- {
- getline(fin, s);
- cout << i << ": " << s << endl;
- }
- fin.close();
- }
- /*Ввод текста в файл*/
- void inputInFile()
- {
- cout << "Ввод в файл: \n";
- string s;
- bool flag = true;
- ofstream fout("data.txt", ios_base::out | ios_base::app);
- cout << "Когда закончите, введите +: " << endl;
- cin.ignore();
- while (getline(cin, s)) {
- if (s[0] == '+' && s.size() == 1) {
- break;
- }
- fout << s << endl;
- }
- fout.close();
- }
- void clearFile() {
- cout << "Файл успешно очищен! \n";
- ofstream fclear("data.txt", ios_base::trunc);
- fclear.close();
- }
- int countLines() {
- string s;
- int cnt = 0;
- ifstream fin("data.txt");
- while (!fin.eof())
- {
- getline(fin, s);
- cnt++;
- }
- return cnt;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement