does anybody know how to create binary files instead of plain text files?
Thanks
-Chris
does anybody know how to create binary files instead of plain text files?
Thanks
-Chris
just do file io operations in binary mode...
hasafraggin shizigishin oppashigger...
when you open a file [with fopen, or whatever...] they give you a parameter in which you can specify the access [read/write/append/create/etc...] and you have a binary/text flag you can set too... depending on what functions you use to do file io... which do you use?
hasafraggin shizigishin oppashigger...
you could write a function that would convert each character to a binary string, but I'm sure it's been done before. I'm not extremely familiar with all the printf() family of functions' conversions for printing integers in various different bases, but there might be somthing in there for printing them as binary strings...you'd have to look at the printf() man pages or help files in whatever compiler you use. You would just make convert the character to it's ASCII value (an integer) and then print that if printf() has anything like that. If not you would need something like the following to do it manually:
void convertToBinary(char oldString[], char newString[][8]) {
double *****eck;
int bitPos = 0;
char tmpString[strlen(oldString)];
strcpy(tmpString, oldString);
for(int i=0; i<strlen(oldString); i++) {
while(tmpString[i]<pow(2, 8-bitPos)-1) {
newString[i][bitPos] = 0;
bitPos++;
}
while(bitPos < 8) {
tmpString = *****eck = double(tmpString[i])/2.0;
// I've never tried this before but I think oldString will get an integer value of
// the divide, and *****eck will get the double value of it.
if(*****eck - int(*****eck) > 0) newString[i][bitPos++] = 1;
else newString[i][bitPos++] = 0;
}
}
}
NOTE: I haven't compiled this, it's probably got errors
So, once you've worked out all the bugs and errors from the above function, it should convert the ASCII string that you pass as oldString into a binary string, that you pass as newString. The form that the binary string takes is given below. This assumes that the first dimension of the two dimensional array, newString is strlen(oldString). I use 8 as the size of the second dimension because a character has 8 bits (0 - 255 in decimal)
binary string form:
example:
oldString = "cat"
------------------------
newString:
[0][0] = 0
[0][1] = 1
[0][2] = 1
[0][3] = 0
[0][4] = 0
[0][5] = 0
[0][6] = 1
[0][7] = 1
end of first character
[1][0] = 0
[1][1] = 1
[1][2] = 1
[1][3] = 0
[1][4] = 0
[1][5] = 0
[1][6] = 0
[1][7] = 1
end of second character
[2][0] = 0
[2][1] = 1
[2][2] = 1
[2][3] = 1
[2][4] = 0
[2][5] = 1
[2][6] = 0
[2][7] = 0
end of third character
all together:
cat = 01100011 01100001 01110100 (no spaces though)
An easy way to print this then would be to overlaod the << operator for two dimensional arrays, if you need help with that ask...not to tough to look up though. Then simply outFile << newString;I just remembered, chars. are only 4 bits, so everywhere I use 8 use 4 instead, unless you are using an unsigned char, which would be 0 - 255, otherwise it's 0-127 = 4 bits. With unsigned chars you could convert a into bitmap to binary for example...anyways it's getting late, early class, not a good combo.
>just do file io operations in binary mode...
why didn't you post 5 mins. earlier?
i did! ::note to ender for a note to self: take speed typing classes... and summary classes...::
hasafraggin shizigishin oppashigger...
this is yet another waste of time to read from yours truly:
yeah, so where it has the astericks in my code above, that's not a pointer to a pointer to a pointer to a pointer...etc, the variable name was bit Check, the filter thought I was attempting to swear
LOL>.>>>>>> THAT'S FUNNY!!!
hasafraggin shizigishin oppashigger...
so I'm long winded, sue me I was converting b10 to b2...Originally posted by doubleanti
i did! ::note to ender for a note to self: take speed typing classes... and summary classes...::
To create binary files, you can either use the c++ type streams or the c functions fopen(), fwrite(), fputc().
Here's an example of writing one integer to a file, then reading it back, using c++ streams.
Code:#include <iostream> #include <fstream> using namespace std; int main() { int i = 65535; int j; char *filename = "b.dat"; ofstream out(filename,ios::binary); out.write((char *) &i,sizeof(int)); out.close(); ifstream in(filename,ios::binary); in.read((char *) &j,sizeof(int)); cout << "j:" << j << endl; in.close(); return 0; }
Last edited by swoopy; 11-12-2001 at 01:02 AM.