Advertisement
titov_ns

task_3

Apr 9th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9. typedef struct informationAboutPerson
  10. {
  11.     string fullName;
  12.     string address;
  13.     int phoneNumber = 0;
  14. } person;
  15.  
  16.  
  17. int numberOfLinesInFile(string filePath);
  18. bool fillArrayOfStructures(person* someArray, const int ARRAY_SIZE, string filePath);
  19. void fillFile(string filePath, person* someArray, const int ARRAY_SIZE, int patternNumber);
  20.  
  21.  
  22.  
  23.  
  24.  
  25. void main()
  26. {
  27.     cout << "Enter two digit number: ";
  28.     int patternNumber;
  29.     cin >> patternNumber;
  30.  
  31.     /* на каждого человека по 3 строчки */
  32.     int numberOfPeople = numberOfLinesInFile("files\\telephoneDirectory.txt") / 3;
  33.  
  34.     person* people = new person[numberOfPeople];
  35.  
  36.     fillArrayOfStructures(people, numberOfPeople, "files\\telephoneDirectory.txt");
  37.     fillFile("files\\newTelephoneDirectory.txt", people, numberOfPeople, patternNumber);
  38.  
  39.     delete[] people;
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46. int numberOfLinesInFile(string filePath)
  47. {
  48.     ifstream file(filePath);
  49.  
  50.     if (!file.is_open())
  51.         return -1;
  52.  
  53.     else
  54.     {
  55.         int numberOfLines = 0;
  56.         string temp;
  57.  
  58.         while (!file.eof())
  59.         {
  60.             getline(file, temp);
  61.  
  62.             /* пустые строки буду игнорировать */
  63.             if (temp != "")
  64.                 numberOfLines++;
  65.         }
  66.  
  67.         file.close();
  68.         return numberOfLines;
  69.     }
  70. }
  71.  
  72.  
  73. /* заполнение массива структур с помощью файла с данными */
  74. bool fillArrayOfStructures(person* someArray, const int ARRAY_SIZE, string filePath)
  75. {
  76.     ifstream file(filePath);
  77.  
  78.     if (!file.is_open())
  79.         return false;
  80.  
  81.     else
  82.     {
  83.         string temp;
  84.  
  85.         for (int i = 0; i < ARRAY_SIZE; i++)
  86.         {
  87.             getline(file, someArray[i].fullName);
  88.             getline(file, someArray[i].address);
  89.             file >> someArray[i].phoneNumber;
  90.  
  91.             /* забирает две "\n" */
  92.             getline(file, temp);
  93.             getline(file, temp);
  94.         }
  95.     }
  96.  
  97.     file.close();
  98.     return true;
  99. }
  100.  
  101.  
  102. /* заполнение нового файла нужными данными из массива структур */
  103. void fillFile(string filePath, person* someArray, const int ARRAY_SIZE, int patternNumber)
  104. {
  105.     ofstream file(filePath);
  106.  
  107.     for (int i = 0; i < ARRAY_SIZE; i++)
  108.     {
  109.         if (someArray[i].phoneNumber / 10000 == patternNumber)
  110.             file << someArray[i].fullName << "\n" << someArray[i].address << "\n" << someArray[i].phoneNumber << "\n\n";
  111.     }
  112.  
  113.     file.close();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement