0

I downloaded the capnproto for dlanguage and started tinkering the sample-addressbook.. My problem is, everytime I add a person to the addressbook, it still prints only one of the data in the file, instead of the whole file..

void writeAddressBook()
{
    MessageBuilder message = new MessageBuilder();
    auto addressbook = message.initRoot!AddressBook;
    auto people = addressbook.initPeople(1);

    auto person = people[0];
    person.id = 123;
    person.setName("Tomas");
    person.email = "tomas@example.com";

    auto personPhones = person.initPhones(1);
    personPhones[0].number = "0917xxxxxxx";
    personPhones[0].type = Person.PhoneNumber.Type.mobile;
    person.employment.school = "School of Thoughts";
    File f = File("1.dat","a+b");
    SerializePacked.writeToUnbuffered(new FileDescriptor(f), message);
}

If I call the writeAddressBook() 4 times, then I have 4 People in the addressbook with the same name, The Problem is, everytime I print all of the data, it only prints the first one..

void printAddressBook()
{
    File f = File("1.dat","r");
    auto message = SerializePacked.readFromUnbuffered(new FileDescriptor(f));

    auto addressbook = message.getRoot!AddressBook;

    foreach(person; addressbook.people)
    {
        writefln("%s: %s", person.name, person.email);

        foreach(phone; person.phones)
        {
            string typeName = "UNKNOWN";
            switch(phone.type) with(Person.PhoneNumber.Type)
            {
                case mobile:
                    typeName = "mobile";
                    break;
                case home:
                    typeName = "home";
                    break;
                case work:
                    typeName = "work";
                    break;
                default:
                    break;
            }
            writefln("  %s phone: %s", typeName, phone.number);
        }

        auto employment = person.employment;
        switch(employment.which()) with(Person.Employment.Which)
        {
            case unemployed:
                writefln("  unemployed");
                break;
            case employer:
                writefln("  employer: %s", employment.employer);
                break;
            case school:
                writefln("  student at: %s", employment.school);
                break;
            case selfEmployed:
                writefln("  self-employed");
                break;
            default:
                break;
        }
    }
}
Tomas
  • 33
  • 4
  • I'm not familiar with the library you're using, but your `writeAddressBook` seems not to add a new person to the book, but overwrite the first person in there, so you don't actually have 4 people in the book, just 1, overwritten 3 times. Are you sure the code does what you think it does? – Michail Aug 22 '17 at 19:42
  • a+b is append binary, So i am sure there are 4 people there, also I can see the file growing, everytime I call the writeAddressBook.. – Tomas Aug 23 '17 at 04:39
  • Hmm. Could you edit your functions to log which of the entries gets printed? It might provide a clue. Also, do you use structs or classes for person and phone types? If the answer is structs, then there's a problem in `writeAddessBook`: `auto person = people[0];` copies `people[0]` and the work below is done on the copy, without affecting original struct. Same goes for `personPhones`. – Michail Aug 23 '17 at 19:34
  • yes they are all structs, I will try this on saturday though thanks, I'll let you know, once I'm going blind again, I really need fresh set of eyes... – Tomas Aug 24 '17 at 05:05

0 Answers0