-
March 17th, 2006, 04:40 AM
#1
Urgent
i am converting a integer number into 10 bit binary number and storing it in the following array
char binary_no[10];
binary_no[i] has either 0 or 1
then i am writing it in a *.txt file using following function
FileText.Write( binary_no,sizeof(binary_no));
using above method will take 10 byte to store the number in a file
but i don't want to waste the memory i want to store the binary number in 10 bits only.
Last edited by punitpandia; March 17th, 2006 at 06:27 AM.
Reason: space constraint
-
March 17th, 2006, 05:41 AM
#2
Re: Urgent
You can't have a file with 10 bits, because the smallest possible memory size which can be allocated is a byte. But you can create a file with 16 bits and use only the first 10. You just need to convert it to the appropiate format using bit-wise operators.
Please don't forget to rate users who helped you!
-
March 17th, 2006, 05:51 AM
#3
Re: Urgent
Originally Posted by punitpandia
please tell me is there any other way to store the number so that size of file is 10bit only.
Just out of curiosity, WHY?
Cheers
-
March 17th, 2006, 06:00 AM
#4
Re: Urgent
golanshahar has asked a very good question. If something seems so complicated that's one of the questions we should ask ourselves.
in your case you've got
Code:
char binary_no[10];
Each char will take a byte in the first place. Secondly when you write some to a text file you are writing characters. If you want to put a '0' or '1' although in your mind you've got them as binary they will go in the file as characters each taking a byte.
If you need to minimize on the storage perhaps you can use serialization. In any case has it has been said you will have to put at least a byte (16 bits) but that's not too much to ask for.
I guess you could use the same approach using a text file. In which case all you'd need to do is convert the number into a character (in your case probably a unicode character as your number could go up to 512 if i got the maths right) and then write the character away.
Hope this helps
-
March 17th, 2006, 06:20 AM
#5
Re: Urgent
i know i can't have file of less than 1 byte
but i will be storing such 6000 numbers of 10 bit each
-
March 17th, 2006, 06:30 AM
#6
Re: Urgent
I put my hands up. I got the maths completely wrong earlier. I only considered the 10th bit.
You seem to have good reason to be concerned about storage. In any case whatever the number you will need at the most 2 bytes for each number. If you use unicode encoding perhaps you can convert your number into a unicode character and write the characters in the file. When you need to read the numbers from the file just do the opposite.
-
March 17th, 2006, 06:41 AM
#7
Re: Urgent
Originally Posted by golanshahar
Just out of curiosity, WHY?
Cheers
Because the standard sais that the smallest addressable entity the byte is.
A general note: the minimum size a file can have is platform dependant and file system format dependant. This because the smalles unit the OS can allocate on the disk a block. Yout 10 byte file will take something like 4k or more on the disk.
@punitpandia: please choose meaningful titles for your threads.
-
March 17th, 2006, 06:51 AM
#8
Re: Urgent
Originally Posted by punitpandia
i know i can't have file of less than 1 byte
but i will be storing such 6000 numbers of 10 bit each
Assuming that you use 16 bits for such a number, then you will use 12000 bytes disk storage. The os will allocate at least one block for that -- so you spend something between 12 k and 32k or so. That is not a problem nowdays, is it?
However, just for the sake of the theory: You can save a byte stream to the file, storing each number on exactly 10 bits, like this:
Code:
01234567012345670123456701234567
¦------¦¦------¦¦------¦¦------¦¦....
byte 1 byte 2 byte 3 byte 4
¦--------¦¦--------¦¦--------¦¦-....
number 1 number 2 number 3
The idea is to transfor the number stream into the byte stream when storing and back when loading the file.
-
March 17th, 2006, 06:59 AM
#9
Re: Urgent
The idea is to transfor the number stream into the byte stream when storing and back when loading the file.
How easy would this be considering that some numbers can be stored using just 1 byte while others will need 2 bytes? I personally think that he might be better off declaring an array of wide characters, putting all the numbers in the array and write in the file. Reading the data is just as easy, every 2 bytes will represent a number that he could easily retrieve from the file.
-
March 17th, 2006, 07:15 AM
#10
Re: Urgent
Originally Posted by Gabriel Fleseriu
Because the standard sais that the smallest addressable entity the byte is.
A general note: the minimum size a file can have is platform dependant and file system format dependant. This because the smalles unit the OS can allocate on the disk a block. Yout 10 byte file will take something like 4k or more on the disk.
My question was directed to OP why he wants to write a file that has size of couple of bits!
I am aware to the fact it can’t be done due to alignment issue
Cheers
Last edited by golanshahar; March 17th, 2006 at 07:20 AM.
-
March 17th, 2006, 08:14 AM
#11
Re: Urgent
Originally Posted by punitpandia
i know i can't have file of less than 1 byte
but i will be storing such 6000 numbers of 10 bit each
This post is showing to me that you don't really understand some basic fundamentals. What is the maximum value of your number? A char is a byte not a bit. Why are you concerned about saving disk space with such a small amount of data? If it were me, I'd just store the number in an array of short and write that to the file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|