Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- typedef struct informationAboutPerson
- {
- string fullName;
- string address;
- int phoneNumber = 0;
- } person;
- int numberOfLinesInFile(string filePath);
- bool fillArrayOfStructures(person* someArray, const int ARRAY_SIZE, string filePath);
- void fillFile(string filePath, person* someArray, const int ARRAY_SIZE, int patternNumber);
- void main()
- {
- cout << "Enter two digit number: ";
- int patternNumber;
- cin >> patternNumber;
- /* на каждого человека по 3 строчки */
- int numberOfPeople = numberOfLinesInFile("files\\telephoneDirectory.txt") / 3;
- person* people = new person[numberOfPeople];
- fillArrayOfStructures(people, numberOfPeople, "files\\telephoneDirectory.txt");
- fillFile("files\\newTelephoneDirectory.txt", people, numberOfPeople, patternNumber);
- delete[] people;
- }
- int numberOfLinesInFile(string filePath)
- {
- ifstream file(filePath);
- if (!file.is_open())
- return -1;
- else
- {
- int numberOfLines = 0;
- string temp;
- while (!file.eof())
- {
- getline(file, temp);
- /* пустые строки буду игнорировать */
- if (temp != "")
- numberOfLines++;
- }
- file.close();
- return numberOfLines;
- }
- }
- /* заполнение массива структур с помощью файла с данными */
- bool fillArrayOfStructures(person* someArray, const int ARRAY_SIZE, string filePath)
- {
- ifstream file(filePath);
- if (!file.is_open())
- return false;
- else
- {
- string temp;
- for (int i = 0; i < ARRAY_SIZE; i++)
- {
- getline(file, someArray[i].fullName);
- getline(file, someArray[i].address);
- file >> someArray[i].phoneNumber;
- /* забирает две "\n" */
- getline(file, temp);
- getline(file, temp);
- }
- }
- file.close();
- return true;
- }
- /* заполнение нового файла нужными данными из массива структур */
- void fillFile(string filePath, person* someArray, const int ARRAY_SIZE, int patternNumber)
- {
- ofstream file(filePath);
- for (int i = 0; i < ARRAY_SIZE; i++)
- {
- if (someArray[i].phoneNumber / 10000 == patternNumber)
- file << someArray[i].fullName << "\n" << someArray[i].address << "\n" << someArray[i].phoneNumber << "\n\n";
- }
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement