I have an application where number of messages is a priory unknown. I can 
open output file and write 0 in the beginning:

Writer::Writer(const string &filename):
    _output(filename.c_str(), ios::out | ios::trunc | ios::binary),
    _events_written(0)
{
    _raw_out.reset(new 
::google::protobuf::io::OstreamOutputStream(&_output));
    _coded_out.reset(new 
::google::protobuf::io::CodedOutputStream(_raw_out.get()));

    _coded_out->WriteVarint32(_events_written);
}

and then in the destructor reset the position in the ofstream to the 
beginning to write the number of the events:

Writer::~Writer()
{
    _coded_out.reset();
    _raw_out.reset();

    _output.seekp(0);

    // Small trick to save number of the events
    //
    _raw_out.reset(new 
::google::protobuf::io::OstreamOutputStream(&_output));
    _coded_out.reset(new 
::google::protobuf::io::CodedOutputStream(_raw_out.get()));

    _coded_out->WriteVarint32(_events_written);

    _coded_out.reset();
    _raw_out.reset();

    _output.close();
}

Then read the beginning of the file with number of the events:

Reader::Reader(const string &filename):
    _input(filename.c_str(), ios::in | ios::binary),
    _is_good(true),
    _events_written(0)
{
    _raw_in.reset(new ::google::protobuf::io::IstreamInputStream(&_input));
    _coded_in.reset(new 
::google::protobuf::io::CodedInputStream(_raw_in.get()));

    _coded_in->ReadVarint32(&_events_written);
}

Everything seems fine. Then events can be read one by one like:

bool Reader::read(Event &event)
{
    event.Clear();

    uint32_t message_size;
    if (!_coded_in->ReadVarint32(&message_size))
    {
        _is_good = false;

        return false;
    }

    if (0 < message_size)
    {
        string message;
        if (!_coded_in->ReadString(&message, message_size) ||
            !event.ParseFromString(message))

            return false;
    }

    return true;
}

Unfortunately, ReadVarint32 fails for some reason in the Reader::read(...) 
method.

The code works fine in case I do not use seekp in the Writer::~Writer().

What is the proper way to seek to the beginning of the file and store number 
of entries?

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to