CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Urgent

  1. #1
    Join Date
    Mar 2006
    Posts
    26

    Resolved 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

  2. #2
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    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!

  3. #3
    Join Date
    May 2005
    Posts
    4,954

    Re: Urgent

    Quote 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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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

  5. #5
    Join Date
    Mar 2006
    Posts
    26

    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

  6. #6
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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.

  7. #7
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Urgent

    Quote 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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  8. #8
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Urgent

    Quote 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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  9. #9
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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.

  10. #10
    Join Date
    May 2005
    Posts
    4,954

    Re: Urgent

    Quote 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.
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,597

    Re: Urgent

    Quote 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)