Thread: Bits in files

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    17

    Bits in files

    Hey everyone,

    I've been doing some looking at writing out in binary form to files and i think it looks like a byte is the smallest ammount

    of info that can be written to a file. Is this true?

    I want to store some info in a particular configuration in a file. ex.

    Say i want to store the info

    Gate 1, Number 3, color green
    Gate 2, Number 5, color red

    instead of storing the info on each line i want to store it vertically

    1 2

    3 5

    g r
    r e
    e d
    e
    n

    but i also have a key that denotes green as 1, and red as 2.

    so my info looks like

    1 2

    3 5

    1 2

    but i want this to be in binary form (it makes the subsequent reading of these files easier)

    1 0
    0 1
    0 0

    1 0
    1 1
    0 1

    1 0
    0 1
    0 0

    (i only have the space in between bits to show what i mean).

    I've looked at the write command but a character is the smallest thing it appears to write (the same as put).

    Has anyone ever needed to have bits in exact positions in a file outputted by a C++ program before?

    If i have a large number of arrays storing the binary of each number and writing bit 0 across each line, then bit 1, then bit

    2 and so forth, is it actually writing binary 1s and 0s or the character 1, and the character 0? 1 bit is alot smaller than a

    1 byte character so i'm hoping to find a way of writing the bits.

    I've written some code

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	ofstream outFile;
    
    	char file[9] = "out3.bin";
    
    	int a = 0;
    	int b = 1;
    
    	outFile.open(file, ios::out | ios::binary | ios::app);
    
    	outFile.write((char *) &a, sizeof(a));
    	outFile.write((char *) &b, sizeof(b));
    	outFile.write((char *) &a, sizeof(a));
    	outFile.write((char *) &b, sizeof(b));
    	outFile.write((char *) &a, sizeof(a));
    
    	outFile.close();
    
    	return 0;
    }
    Which shud place 01010 into file out3.bin, if i try and view this file it's jibberish. Shud i be able to see 01010 on screen?

    Also how do i write to the next line in binary mode, do i have to use seekp - just wonderin if there's an easier way to just

    say next line or something (using seek may take 2 or 3 lines of code instead of a potentially handy one).


    Cheers,

    Rob.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Some thoughts...

    You're right. You can't read/write a single bit.

    The "binary" file format doesn't really mean binary... it means anything but text. Actually, it could be text, but you'd have to format the linefeed/carrrage return manually.

    And, "store it vertically" is confusing to me. All the characters & variables are going to be stored sequentially. You can read them back and display them in your preferred format.

    You have to do your base-convesion during input-output... to the C++ program it's just a char, or an int (which, ironically is stored in binary-bits!) So you have to tell the program "here's a number in binary format", and "display this number in binary format".

    Binary input-output is tricky in C++ (Hex and octal are easier.) One way is to use <bitset.h>. There is another method using enumerated variables... but the technique escapes me at the moment.... I think it's similar to your a=0, b=1 thechnique, where each bit is stored as a whole char or int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM