Writing bits to a file in C

前端 未结 5 372
时光说笑
时光说笑 2021-02-03 22:14

I have this string: \"101\" I want to write it to a file, in C, not as text: \"101\" and so 8 bits x char. but directly use the string as bits: the bit \"1\", the bit \"0\" and

相关标签:
5条回答
  • 2021-02-03 22:55

    All filesystems¹ deal with files in terms of bytes (and allocate storage space with a much larger granularity, 512 bytes at a time minimum). There's no way you are going to get a file that is 3 bits long.

    The best you can do is use up one whole byte but ignore 5 of its bits. To do that (assuming that the number is always going to fit into a byte), convert the input string to an integral type:

    long l = strtol(c, 0, 2);
    

    Then get its least significant byte:

    unsigned char b = l & 0xffl;
    

    And write it to the file:

    fwrite(&b, 1, 1, binFile);
    

    ¹ Well, maybe not all. There might be some researchers somewhere that experiment with bit-sized filesystems. I wouldn't know.

    0 讨论(0)
  • 2021-02-03 22:57

    2 notes on your approach:

    1. [Modern] Computers can't handle less than 1 byte in memory, so you won't be able to write single bits to disk.

      Also, filesystems usually allocate space in chunks (512 bytes, 1Kb, ...) where the file fits. So, if you have a 500 bytes file, you are actually loosing 512 bytes of disk space.

    2. atoi() doesn't convert from string to binary numbers, but to integer. You are actually writing 0b1100101, which is 0d101. You should do the conversion first. Something like:

      char b = 0;
      for (int i=0; c[i]!=NULL; i++) 
      {
          b = ((b<<1) | atoi(c[i]));
      }
      
    0 讨论(0)
  • 2021-02-03 23:07

    You are not writing the bits 101, but the binary value of the decimal number 101, ie 1100101. Because you are fread a size of sizeof(x) bytes, your file will be sizeof(x) bytes long.

    0 讨论(0)
  • 2021-02-03 23:13

    Your output file is 4 bytes in length because you're writing an int to the file. On most platforms, an int is 4 bytes in size.

    You won't be able to write less than 1 byte at a time.

    0 讨论(0)
  • 2021-02-03 23:13

    What you can do is write it as bits (3) and fill it up to a byte with 0's. However, you also need to start (or end) with the number of bits (or bits from the last byte) that are really used.

    E.g. (using first byte as length):

    00000011   -> 3, meaning from the last (and only byte in this case, 
                  only the first 3 bits are used)
    10100000   -> 101 is the string, other 5 bits are 0, just use for padding
    

    In this case the overhead of the first (length) byte is 50%, the longer the string, the less the overhead percentage of course.

    0 讨论(0)
提交回复
热议问题