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 964749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:52:52+00:00 2026-05-16T01:52:52+00:00

How can I write only every third item in a char buffer to file

  • 0

How can I write only every third item in a char buffer to file quickly in C++?

I get a three-channel image from my camera, but each channel contains the same info (the image is grayscale). I’d like to write only one channel to disk to save space and make the writes faster, since this is part of a real-time, data collection system.

C++’s ofstream::write command seems to only write contiguous blocks of binary data, so my current code writes all three channels and runs too slowly:

char * data = getDataFromCamera();
int dataSize = imageWidth * imageHeight * imageChannels;
std::ofstream output;
output.open( fileName, std::ios::out | std::ios::binary );
output.write( data, dataSize );

I’d love to be able to replace the last line with a call like:

int skipSize = imageChannels;
output.write( data, dataSize, skipSize );

where skipSize would cause write to put only every third into the output file. However, I haven’t been able to find any function that does this.

I’d love to hear any ideas for getting a single channel written to disk quickly.
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-16T01:52:52+00:00Added an answer on May 16, 2026 at 1:52 am

    Let’s say your buffer is 24-bit RGB, and you’re using a 32-bit processor (so that operations on 32-bit entities are the most efficient).

    For the most speed, let’s work with a 12-byte chunk at a time. In twelve bytes, we’ll have 4 pixels, like so:

    AAABBBCCCDDD
    

    Which is 3 32-bit values:

    AAAB
    BBCC
    CDDD
    

    We want to turn this into ABCD (a single 32-bit value).

    We can create ABCD by applying a mask to each input and ORing.

    ABCD = A000 | 0BC0 | 000D
    

    In C++, with a little-endian processor, I think it would be:

    unsigned int turn12grayBytesInto4ColorBytes( unsigned int buf[3] )
    {
       return (buf[0]&0x000000FF) // mask seems reversed because of little-endianness
            | (buf[1]&0x00FFFF00)
            | (buf[2]&0xFF000000);
    }
    

    It’s probably fastest to do this another conversion to another buffer and THEN dump to disk, instead of going directly to disk.

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

Sidebar

Related Questions

Can we write files to the file system and leave them as read-only in
irb can write command history to a file, but it only does this when
How can we write html tidy coding only for inserting closing tag in the
How can I write a regular expression in javascript that only allows users to
I want to write an application that gets events on every file change on
Instead of pasting $('html, body').animate({ scrollTop: 0}, 2500); for every input, can I write
I wrote some codes so as to send e mail but I can only
You can write //google.com instead of http://google.com and https://google.com where // is relative to
I can write an assertion message one of two ways. Stating success: assertEquals( objects
We can write CSS as the following types: Inline CSS Embedded CSS External CSS

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.