Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <Windows.h>
- using namespace std;
- struct contact {
- char name[40];
- char mobile_phone[20];
- char home_phone[20];
- char work_phone[20];
- };
- void add_contact(const char* name, const char* mobile_phone, const char* home_phone, const char* work_phone) {
- ofstream fout;
- fout.open("base.txt", ios::app);
- if (!fout.is_open()) {
- cout << "Ошибка открытия файла" << endl;
- }
- else {
- fout << name << endl << mobile_phone << endl << home_phone << endl << work_phone << endl << endl;
- fout.close();
- }
- }
- void del_contact(const char* name) {
- ifstream fin;
- ofstream fout;
- fin.open("base.txt");
- fout.open("base2.txt");
- if (!fin.is_open() || !fout.is_open()) {
- cout << "Ошибка открытия файла" << endl;
- }
- else {
- contact cont;
- while (!fin.eof()) {
- fin >> cont.name >> cont.mobile_phone >> cont.home_phone >> cont.work_phone;
- if (strcmp(name, cont.name)) {
- fout << cont.name << endl << cont.mobile_phone << endl << cont.home_phone << endl << cont.work_phone << endl << endl;
- }
- }
- fin.close();
- fout.close();
- remove("base.txt");
- rename("base2.txt", "base.txt");
- }
- }
- void find_contact_name(const char* name) {
- ifstream fin;
- fin.open("base.txt");
- if (!fin.is_open()) {
- cout << "Ошибка открытия файла" << endl;
- }
- else {
- contact cont;
- while (!fin.eof()) {
- fin >> cont.name >> cont.mobile_phone >> cont.home_phone >> cont.work_phone;
- if (!strcmp(name, cont.name)) {
- cout << cont.name << endl << cont.mobile_phone << endl << cont.home_phone << endl << cont.work_phone << endl << endl;
- }
- }
- fin.close();
- }
- }
- void show_all_contacts() {
- ifstream fin;
- fin.open("base.txt");
- if (!fin.is_open()) {
- cout << "Ошибка открытия файла" << endl;
- }
- else {
- contact cont;
- while (!fin.eof()) {
- fin >> cont.name >> cont.mobile_phone >> cont.home_phone >> cont.work_phone;
- cout << cont.name << endl << cont.mobile_phone << endl << cont.home_phone << endl << cont.work_phone << endl << endl;
- }
- fin.close();
- }
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- add_contact("Иван", "+79111111111", "111111", "222222");
- add_contact("Оля", "+79111114444", "111144", "222244");
- add_contact("Женя", "+79111115555", "111155", "222255");
- add_contact("Саша", "+79111116666", "111166", "222266");
- show_all_contacts();
- del_contact("Женя");
- show_all_contacts();
- find_contact_name("Саша");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement