Advertisement
35657

Untitled

May 25th, 2024
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6. #include <Windows.h>
  7.  
  8. using namespace std;
  9.  
  10. struct contact {
  11.     char name[40];
  12.     char mobile_phone[20];
  13.     char home_phone[20];
  14.     char work_phone[20];
  15. };
  16.  
  17.  
  18. void add_contact(const char* name, const char* mobile_phone, const char* home_phone, const char* work_phone) {
  19.  
  20.     ofstream fout;
  21.     fout.open("base.txt", ios::app);
  22.  
  23.     if (!fout.is_open()) {
  24.         cout << "Ошибка открытия файла" << endl;
  25.     }
  26.     else {
  27.         fout << name << endl << mobile_phone << endl << home_phone << endl << work_phone << endl << endl;
  28.  
  29.         fout.close();
  30.     }
  31.  
  32. }
  33.  
  34. void del_contact(const char* name) {
  35.  
  36.     ifstream fin;
  37.     ofstream fout;
  38.     fin.open("base.txt");
  39.     fout.open("base2.txt");
  40.  
  41.     if (!fin.is_open() || !fout.is_open()) {
  42.         cout << "Ошибка открытия файла" << endl;
  43.     }
  44.     else {
  45.  
  46.         contact cont;
  47.  
  48.         while (!fin.eof()) {
  49.             fin >> cont.name >> cont.mobile_phone >> cont.home_phone >> cont.work_phone;
  50.  
  51.             if (strcmp(name, cont.name)) {
  52.                 fout << cont.name << endl << cont.mobile_phone << endl << cont.home_phone << endl << cont.work_phone << endl << endl;
  53.             }
  54.         }
  55.         fin.close();
  56.         fout.close();
  57.         remove("base.txt");
  58.         rename("base2.txt", "base.txt");
  59.     }
  60. }
  61.  
  62. void find_contact_name(const char* name) {
  63.     ifstream fin;
  64.     fin.open("base.txt");
  65.  
  66.     if (!fin.is_open()) {
  67.         cout << "Ошибка открытия файла" << endl;
  68.     }
  69.     else {
  70.  
  71.         contact cont;
  72.  
  73.         while (!fin.eof()) {
  74.             fin >> cont.name >> cont.mobile_phone >> cont.home_phone >> cont.work_phone;
  75.  
  76.             if (!strcmp(name, cont.name)) {
  77.                 cout << cont.name << endl << cont.mobile_phone << endl << cont.home_phone << endl << cont.work_phone << endl << endl;
  78.             }
  79.         }
  80.         fin.close();
  81.     }
  82. }
  83.  
  84. void show_all_contacts() {
  85.     ifstream fin;
  86.     fin.open("base.txt");
  87.  
  88.     if (!fin.is_open()) {
  89.         cout << "Ошибка открытия файла" << endl;
  90.     }
  91.     else {
  92.  
  93.         contact cont;
  94.  
  95.         while (!fin.eof()) {
  96.             fin >> cont.name >> cont.mobile_phone >> cont.home_phone >> cont.work_phone;
  97.  
  98.             cout << cont.name << endl << cont.mobile_phone << endl << cont.home_phone << endl << cont.work_phone << endl << endl;
  99.         }
  100.         fin.close();
  101.     }
  102. }
  103.  
  104. int main() {
  105.  
  106.     SetConsoleCP(1251);
  107.     SetConsoleOutputCP(1251);
  108.  
  109.  
  110.     add_contact("Иван", "+79111111111", "111111", "222222");
  111.     add_contact("Оля", "+79111114444", "111144", "222244");
  112.     add_contact("Женя", "+79111115555", "111155", "222255");
  113.     add_contact("Саша", "+79111116666", "111166", "222266");
  114.  
  115.     show_all_contacts();
  116.  
  117.     del_contact("Женя");
  118.  
  119.     show_all_contacts();
  120.  
  121.     find_contact_name("Саша");
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement