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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:47:37+00:00 2026-06-11T13:47:37+00:00

I am writing a program like this to write some bytes into a file.

  • 0

I am writing a program like this to write some bytes into a file.

#include <fstream>

int main()
{
    char buffer[4] = {0, 0, 255, 255};
    std::ofstream f("foo.txt", std::ios_base::binary);
    f.write(buffer, sizeof buffer);
    f.close();
    return 0;
}

This works fine and gives me the expected result on my system.

$ g++ -std=c++11 -pedantic -Wall -Wextra signedness.cc
$ ./a.out
$ cat foo.txt
$ od -t x1 foo.txt 
0000000 00 00 ff ff
0000004

The equivalent C code would be:

#include <stdio.h>

int main()
{
    char buffer[4] = {0, 0, 255, 255};
    FILE *f = fopen("bar.txt", "wb");
    fwrite(buffer, sizeof *buffer, sizeof buffer, f);
    fclose(f);
    return 0;
}

This program also works fine and gives the expected output on my system.

I want to know if the above way of writing bytes into a file is okay.

Section 4.7 (Integral conversions) of C++ n3242.pdf mentions in point 3:

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

Section 6.3.1.3 (signed and unsigned integers) of C n1256.pdf mentions in point 3:

Otherwise, the new type is signed and the value cannot be represented in it; either the
result is implementation-defined or an implementation-defined signal is raised.

From these extracts, it seems like my program invokes implementation-defined behavior when I assign 255 as the last two bytes of char buffer[4] because 255 cannot be represented in char type. If I am right about this, then what would be the right way to write these four bytes into a file? Changing the type of buffer from char to unsigned char doesn’t seem to help in C++ because ofstream’s write() function still expects the first parameter to of type const char*.

  • 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-06-11T13:47:38+00:00Added an answer on June 11, 2026 at 1:47 pm

    Remember that ofstream is just a typedef for std::basic_ofstream<char>.

    When you don’t want things being treated as char, just use std::basic_ofstream<unsigned char> or std::basic_ofstream<uint8_t>.

    Ultimately, though, iostreams are made for formatted I/O. The API is horrible for binary I/O (since it doesn’t take void*) and also incredibly slow. Furthermore, every character is converted by a “facet”, making it hard to guarantee 1:1 correspondence between input bytes and bytes on disk. basic_filebuf is a little better, but not much. Using fopen and fwrite is still a perfectly valid approach, even in C++.

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

Sidebar

Related Questions

I am writing a program based on file. My code looks something like this,
I wrote the following program using VS2008: #include <fstream> int main() { std::wofstream fout(myfile);
I'm writing a program which is more or less like this : #include <list>
I'm writing a blackjack program and would like to be able to write the
Writing a client / server program and previously was using calls like this: TcpListener.AcceptTcpClient()
I am writing a program in assembly and it isn't working, so I'd like
I'm currently writing a program in Python, and I would like to determine the
I'm writing a C program that needs good error handling. The code likes like
For the python program I am writing I would like to give the opportunity
I'd like to add video conversion capabilities to a program I'm writing. FFmpeg's command

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.