Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1022337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:27:01+00:00 2026-05-16T11:27:01+00:00

Background: I’m using Google’s protobuf , and I would like to read/write several gigabytes

  • 0

Background:

I’m using Google’s protobuf, and I would like to read/write several gigabytes of protobuf marshalled data to a file using C++. As it’s recommended to keep the size of each protobuf object under 1MB, I figured a binary stream (illustrated below) written to a file would work. Each offset contains the number of bytes to the next offset until the end of the file is reached. This way, each protobuf can stay under 1MB, and I can glob them together to my heart’s content.

[int32 offset]
[protobuf blob 1]
[int32 offset]
[protobuf blob 2]
...
[eof]

I have an implemntation that works on Github:

src/glob.hpp
src/glob.cpp
test/readglob.cpp
test/writeglob.cpp

But I feel I have written some poor code, and would appreciate some advice on how to improve it. Thus,

Questions:

  • I’m using reinterpret_cast<char*> to read/write the 32 bit integers to and from the binary fstream. Since I’m using protobuf, I’m making the assumption that all machines are little-endian. I also assert that an int is indeed 4 bytes. Is there a better way to read/write a 32 bit integer to a binary fstream given these two limiting assumptions?
  • In reading from fstream, I create a temporary fixed-length char buffer, so that I can then pass this fixed-length buffer to the protobuf library to decode using ParseFromArray, as ParseFromIstream will consume the entire stream. I’d really prefer just to tell the library to read at most the next N bytes from fstream, but there doesn’t seem to be that functionality in protobuf. What would be the most idiomatic way to pass a function at most N bytes of an fstream? Or is my design sufficiently upside down that I should consider a different approach entirely?

Edit:

  • @codymanix: I’m casting to char since istream::read requires a char array if I’m not mistaken. I’m also not using the extraction operator >> since I read it was poor form to use with binary streams. Or is this last piece of advice bogus?
  • @Martin York: Removed new/delete in favor of std::vector<char>. glob.cpp is now updated. Thanks!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:27:02+00:00Added an answer on May 16, 2026 at 11:27 am

    Don’t use new []/delete[].

    Instead us a std::vector as deallocation is guaranteed in the event of exceptions.

    Don’t assume that reading will return all the bytes you requested.
    Check with gcount() to make sure that you got what you asked for.

    Rather than have Glob implement the code for both input and output depending on a switch in the constructor. Rather implement two specialized classes like ifstream/ofstream. This will simplify both the interface and the usage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I would like to dismiss a modalView that I have presented earlier and
Background: Using unix, codeigniter from localhost. I'd like to run a controllers via a
Background: We're building an application that allows our customers to supply data in a
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background - I am using paramiko to put files on a bunch of remote
Background - I want to extract specific columns from a csv file. The csv
Background: I have several builds running on a Windows Server 2003 R2 machine via
BACKGROUND I'm using VS 2010 on a machine where I installed .Net 4.5 which
[background below] I've got my data modelled out in SQLObject in Python on the
Background My project is urgent and requires that I iterate a large XML file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.