You are here:

C++/writing in a file


I am converting a integer number into binary number
and storing it in following array

char number[10];

number[i] has 0 or 1, this way i m forming a 10 bit binary number
but writing  it in a file will take 10 byte
please tell me some other way of storing the binary number so that when i m writing it in a file it takes only 10 bits.

You seem not to have not grasped the fact that when your value is an integer is _is_ a binary value - digital computers count and store data in binary.

However using the most obvious C++ techniques to write to a file, the operator<< functions:

       outstream << myInt;

converts the int to a text format, so you see characters such as:


in the file.

To write the value as a binary value you would have to use the write member function of the output stream:

       const int BufferLength( 4 );
       char buffer[BufferLength];

       // ...

       outstrem.write( buffer, BufferLength );

This takes a char *, a pointer to a buffer of char (which are byte sized in most implementations), and the number of char (bytes) to write from this buffer. To write out integer data you have to convert the address of the integer to a char * using a cast and then write out the number of char in the integer type (the size of the type or variable).

Your first option is to leave your value as an integer. On a modern 32-bit operating system and compiler this will be a 32-bit binary value (4 bytes):

       outstream.write(reinterpret_cast<char*>(&myInt), sizeof myInt);

Note that the use of reinterpret_cast, or any casting, is not generally recommended. However it is in these sorts of situations where you are likely to find casts. There are ways around it, one being the use of a union:

       union IntAndChar
         int i;
         char c;

Which can be used like so:

       int v(1234567890);
       IntAndChar ic;
       ic.i = v;
       outstream.write( &ic.c, sizeof ic.i );

However this is still writing 4 bytes, a better fit for your 10 bit data is 16 bits, and that is a near as you are going to get without things becoming complicated because files are written in chunks of 8-bit bytes.

Your next option is to use a 16-bit integer type for your data. A short int on the same sort of systems and compilers we have been talking about is usually a 16-bit value. Replace your use of int for 10 bit data with short and your program should now write each value as 2 bytes instead of 4.

A warning here: the sizes of the building types in C++ are not that fixed. An int could be 8, 16, 32, 64 bits in size or some less common size such as 24 bits. The same can be said for short int and long int types. All the standard says is that char is no bigger than short which is no bigger than int which is no bigger than long. This implies that it is possible that all these types are the same size. Further, the size of char is _not_ defined to be 8-bits. It is defined to be large enough to hold any member of the implementation's character set. This means that on some exotic implementation all integer types could be the same size and that could be something strange such as 24-bits. And yes, such implementations do exist for such implementations on devices such as digital signal processors (DSPs).

In general though, for most systems you will encounter for desktop, workstation or server application programming char will be 8-bits and short, int and long will range through 16, 32 and 64 bits. On the most common 32-bit platforms char is 8-bits, short is 16-bits, int and long are both 32-bits.

The other problem with binary values greater than one byte in length is which byte in the file maps to which 8-bits of the integer they are a part of?

For example if you write to file the 16-bit value 0x4142 (0x41 is the character A and 0x42 the character B in the ASCII character set) would you see:




in the file?

The answer is that it depends on which processor and possibly operating system you are using. This as I have said to you before is referred to as endianness. The point to be aware of is that writing binary data on a system using one endian type and reading on a system using another renders the data junk. The same applies when transferring binary data between such disparate systems on a network. The solution is to ensure the byte ordering of multi-byte values that are intended to be shared between systems is defined and each implementation ensures it converts to and from this format as required. For some systems this will be a simple do nothing operation, for others it will mean reversing bytes when reading or writing them.

If you really wish to save data as 10-bits then you will have to devise ways to pack and unpack the individual values into a set of bytes. A obvious point to note that 4 10 bit words pack into 5 8 bit bytes. If you do this then I suggest you design and implement a class to handle this data type. Your previous idea of using a std::bitset may be useful as a component in this class, possibly to hold the 4 values as a 40 bit bitset. However this is your problem not mine so I'll leave the details and decisions up to you, I have other things to do. Have fun.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.