0

我下载了 dlanguage 的 capnproto 并开始修改示例地址簿。我的问题是,每次我将一个人添加到地址簿时,它仍然只打印文件中的一个数据,而不是整个文件。

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);
}

如果我调用 writeAddressBook() 4 次,那么地址簿中有 4 个同名的人,问题是,每次我打印所有数据时,它只打印第一个..

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;
        }
    }
}
4

0 回答 0