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();
- bool checkEmptiness();
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- if (!checkEmptiness()) return -1;
- 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) { //удаление i-ой строки
- if (!checkEmptiness()) return;
- listNumeration(countOfLine);
- int d, i = 0;
- string s;
- cout << "\nВведите номер строки, которую вы хотите удалить: ";
- 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"); //переиминовывает 1 файл на второй
- }
- void list(int countOfLine) { //вывод строк из файла на экран
- if (!checkEmptiness()) return;
- int cut, t = 0;
- cout << "Введите значение до которого будет выводится строка\n";
- cin >> cut;
- cut = cut;
- ifstream fin("data.txt");
- // если первый символ конец файла
- cout << "Текст из файла: \n";
- string s;
- int i = 0;
- while (!fin.eof() && ++i < countOfLine) //Будем читать информацию, пока не дойдем до конца файла
- {
- int u = 0, k = t;
- getline(fin, s);
- while (cut > 0) {
- if (u >= s.size()) {
- t = k;
- break;
- }
- else if (k >= cut) {
- cout << endl;
- k = 0;
- }
- cout << s[u++];
- k++;
- }
- }
- cout << endl;
- fin.close();
- }
- void listNumeration(int countOfLine) { //вывод строк с нумерацией
- if (!checkEmptiness()) return;
- 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() { //Ввод текста в файл
- if (!checkEmptiness()) return;
- 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 out() {
- system("CLS");
- int d;
- ifstream fin("data.txt");
- if (!fin) { //Если файл не существует, то
- cout << "А где текст? Так не пойдёт, сначала введите его, а уж потом я разделю на строчки!";
- }
- else {
- cout << "Подскажите длинну строки: ";
- cin >> d;
- string text;
- int c = 0;
- int i = 0;
- getline(fin, s);
- while (c < text.length()) {
- string TX = text.substr(c, d);
- c += d;
- X[i] = new string;
- *X[i] = TX;
- i++;
- }
- for (int j = 0; j < i; j++) cout << *X[j] << endl;
- cr = false;
- }
- fin.close();
- }*/
- void clearFile() { //очистка файла
- cout << "Файл успешно очищен! \n";
- ofstream fclear("data.txt", ios_base::trunc);
- fclear.close();
- }
- int countLines() { //считаем количество строк
- if (!checkEmptiness()) return -1;
- string s;
- int cnt = 0;
- ifstream fin("data.txt");
- while (!fin.eof())
- {
- getline(fin, s);
- cnt++;
- }
- return cnt;
- }
- bool checkEmptiness() {
- bool flag = true;
- ifstream file("data.txt");
- if (file.is_open()) {
- if (file.peek() == EOF) {
- cout << "Ваш файл пуст! Введите текст в файл.\n";
- flag = false;
- }
- file.close();
- }
- else {
- cout << "Файл не существует.\n";
- flag = false;
- }
- return flag;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement