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.