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

  • Home
  • SEARCH
  • 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 7541269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:50:58+00:00 2026-05-30T07:50:58+00:00

I have some data, n elements, uchar* data_ptr. How would I write this to

  • 0

I have some data, n elements, uchar* data_ptr. How would I write this to a binary file and read it again later?

I tried the following :

std::ofstream myFile ("data.bin", std::ios::out | std::ios::binary);
myFile.write(data_ptr,100); 

Above won’t compile. uchar not being char.

How do I write it. And how to I read it into a memory chunk of uchar* again.

I did the following test:

cv::Mat test(10,10,CV_8UC1);
cv::randu(test,0,255);
std::cout << test << std::endl;
assert(test.isContinuous());

std::ofstream myFile1 ("data1.bin", std::ios::out | std::ios::binary);
myFile1.write(reinterpret_cast<char*>(test.data),sizeof(uchar)*100);

uchar buf[100];
std::ifstream myFile ("data1.bin", std::ios::in | std::ios::binary);
myFile.read(reinterpret_cast<char*>(buf), sizeof(buf));
cv::Mat test1(10,10,CV_8UC1,buf);
std::cout << test1 << std::endl;

cv::waitKey();

And got the following output:

[91, 2, 79, 179, 52, 205, 236, 8, 181, 239;
  26, 248, 207, 218, 45, 183, 158, 101, 102, 18;
  118, 68, 210, 139, 198, 207, 211, 181, 162, 197;
  191, 196, 40, 7, 243, 230, 45, 6, 48, 173;
  242, 125, 175, 90, 63, 90, 22, 112, 221, 167;
  224, 113, 208, 123, 214, 35, 229, 6, 143, 138;
  98, 81, 118, 187, 167, 140, 218, 178, 23, 43;
  133, 154, 150, 76, 101, 8, 38, 238, 84, 47;
  7, 117, 246, 163, 237, 69, 129, 60, 101, 41;
  190, 50, 90, 72, 168, 109, 121, 220, 114, 248]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 127, 2, 0, 0;
  32, 0, 0, 0, 255, 255, 0, 0, 178, 116;
  36, 101, 35, 0, 28, 5, 248, 227, 87, 0;
  43, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
  0, 0, 205, 18, 246, 118, 251, 10, 251, 118]

Clearly the two matrices are not the same. Any comment on where it went wrong?

  • 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-30T07:50:59+00:00Added an answer on May 30, 2026 at 7:50 am

    To write, you can simply cast the data to char*:

    file.write(reinterpret_cast<char*>(data_ptr), 100); // writes 100 bytes
    

    To read, you need an istream and again a cast is required:

    uchar buf[100];
    input.read(reinterpret_cast<char*>(buf), sizeof(buf));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to login in to various elements to retrieve some data, problem is
I have some data in an Excel Worksheet. I would like to select all
I have some xml data and I am trying to access some elements. The
I have some data that I want to present in a FlowDocument . This
I have some large arrays of 2D data elements. A and B aren't equally
In Python I have some data that looks like this: A = [ (-9,
I have some data that goes over three levels, like this: Identifier, Key, Value
I have some irregular data in one column. my.table <-read.table(text=ticker,date,last a,12/20/2011,289295 a,12/21/2011,NA a,12/27/2011,297001 a,12/28/2011,NA
I have some data with messed-up accented characters. For example in the data we
I have some data grouped in a table by a certain criteria, and for

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.