473,387 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Write short to file in binary format

I have the following:

short x = 3;
...
std:ofstream file(outfile.txt, std::ios::out|std::ios::trunc);
...
file << x << endl;

Of course, "3" shows up in the file. What I'd really like to see is
"00000011". I'd be very surprised to see that C++ doesn't already have an
operator to do this for me.

Thanks,
Randy

Jul 23 '05 #1
2 4220
This will do what you want... there may be a better solution.
You can use the standard library bitset template class.
The templated value is the number of bits ( 8 * sizeof(short) ) will usually
be 16.

#include <fstream>
#include <bitset>
using namespace std;

int main(int argc, char* argv[])
{
short x = 3;
bitset<(8 * sizeof(short))> aByte(x);
ofstream file("outfile.txt", std::ios::out|std::ios::trunc);
file << aByte << endl;
return 0;
}

Good Luck,
Kyle

"randy1200" <ra*******@yahoo.com> wrote in message
news:9b******************************@localhost.ta lkaboutprogramming.com...
I have the following:

short x = 3;
..
std:ofstream file(outfile.txt, std::ios::out|std::ios::trunc);
..
file << x << endl;

Of course, "3" shows up in the file. What I'd really like to see is
"00000011". I'd be very surprised to see that C++ doesn't already have an
operator to do this for me.

Thanks,
Randy

Jul 23 '05 #2
That's exactly what I needed. Many thanks...

Randy

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there...
6
by: Betty Hickman | last post by:
I'm having trouble writing a 2D array to a binary file. Here's what I have: FILE *outfile; short **clump_class; clump_class = malloc(nrows * sizeof(short *)); for (i=0; i<nrows; ++i)...
3
by: garyatusa | last post by:
Hi, there, I need to put a bunch of data to an iostream, or a buffer sequencially for each object, and write them to a file in specific sequence. I couldn't find any helpful information how to...
4
by: Thomi Aurel RUAG A | last post by:
Hy Mike Thanks for your links, unfortunately they weren't very usefull for my specific problem. Hy Grant Edwards Thanks for your hints. A simplified test programm to compare the function for...
6
by: aagarwal8 | last post by:
Hi, I am trying to write the contents of a textbox to a file in binary format. My code looks like this... private void btnWriteToFile_Click(object sender, EventArgs e) { FileStream fs =...
1
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how...
1
by: xiao | last post by:
HI~ guys , I have a program here (Sorry it is very long about 240 lines.) It can read and write the header information successfully but it cannot write the array successfully. I guess there is...
6
by: A.Rocha | last post by:
Hi, I need save to text file a List<type>, but i dont wont serialize. // List<mytypemyList = new List<mytype>(); anyone can help me? --
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.