C++/writing in a file
I am converting a integer number into binary number
and storing it in following array
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 );
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:
Which can be used like so:
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.