I will say in advance that the teacher didn’t say anything about ifstream and it will start from the EP, but at the base they teach everything, but not what is needed.

There are two structures Company and Contacts, contain different fields, I write them in a file like this., The method that records, and the structure itself is created in it.

void Phonebook::AddCompany() { FILE *phoneBook; fopen_s(&phoneBook, "phoneBook.txt", "a"); if (!phoneBook) { printf("error"); exit(1); } Company *company = new Company(); if (company) { CreateCompany(company); fprintf(phoneBook, "%s %s %s %s %s\n", company->companyName, company->specialization, company->phoneNumber, company->address->street, company->address->house); fclose(phoneBook); СleaningPointersCompany(company); } } void Phonebook::AddContact() { FILE *phoneBook; fopen_s(&phoneBook, "phoneBook.txt", "a"); if (!phoneBook) { printf("error"); exit(1); } Individual *contact = new Individual(); if (contact) { CreateContact(contact); fprintf(phoneBook, "%s %s %s %s %s\n", contact->name, contact->surname, contact->phoneNumber, contact->address->street, contact->address->house); fclose(phoneBook); СleaningPointersContacts(contact); } } 

There are no problems with the recording, I get the file "phoneBook.txt". Tell me how to read the data, so that the data that belongs to the Contacts does not fall into the Company, I understand that when there is a file reading there is no spit what type, just a data set, but I don’t apply how to read the mind. In general, the task sounds like this, 1) that all Companies in the console should be output from the file, sort and rewrite, 2) output all contacts, sort as and rewrite. The biggest problem is putting the data correctly in the array to work with it.

    1 answer 1

     "%s %s %s %s %s\n" 

    Write the word company or contact in front of him, and when you read it, check it and create the appropriate class. Anyway, the record is dubious. If at least 2 fields can contain spaces, you cannot parse it. And if one, then you can, but get tired.

    • How would you advise to do it if you can in the form of a sample, otherwise I don’t understand how to read different types of data from one file - Artem