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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:59:12+00:00 2026-05-27T20:59:12+00:00

So far I have code to read an unsigned char from ifstream: ifstream in;

  • 0

So far I have code to read an unsigned char from ifstream:

ifstream in;
unsigned char temp;

in.open ("RANDOMFILE", ios::in | ios::binary);
in.read (&temp, 1);
in.close ();

Is this correct? I also tried to write an unsigned char to an ofstream:

ofstream out;
unsigned char temp;

out.open ("RANDOMFILE", ios::out | ios::binary);
out.write (&static_cast<char>(temp), 1);
out.close ();

But I get the following error for writing:

error C2102: '&' requires l-value

And this error for reading:

error C2664: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'unsigned char *' to 'char *'

It would be appreciated if someone could tell me what’s wrong with my code or how I can read and write unsigned chars from fstream.

  • 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-27T20:59:13+00:00Added an answer on May 27, 2026 at 8:59 pm

    The write error is telling you that you are taking the address of the temporary created by static_cast.

    Instead of:

    // Make a new char with the same value as temp
    out.write (&static_cast<char>(temp), 1);
    

    Use the same data already in temp:

    // Use temp directly, interpreting it as a char
    out.write (reinterpret_cast<char*>(&temp), 1);
    

    The read error will also be fixed if you tell the compiler to interpret the data as a char:

    in.read (reinterpret_cast<char*>(&temp), 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thos piece of code: Math&&Math.random?Math.floor(Math.random()*10000000000000):Date.getTime(); And as far as i know &&
Here is a the code I have so far: from ConfigParser import * import
So far i have got the code below which works lovely when trying an
I have code blindness, it's like snowblindness, just wth far too much code. I
I have this code so far which perfectly but relies on there being a
So far I have managed to write some code that should print the source
I'm a total newb to LINQ. Here is the code I have so far:
This is what I have as far as code, it is in the first
How far is the code for best functionallity? I have two ComboBox, so the
According to what I have found so far, I can use the following code:

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.