How to write to a file in Binary

Started by
11 comments, last by crazy_andy 19 years, 8 months ago
How can you write to a file in binary mode, i worked out the following code, but it doesnt work: FILE *file2 = fopen(filename,"wb"); write("0",1,1,file2); This code just writes 0 to the file I want to create, rather than the binary code 0.
www.stickskate.com -> check it out, some gnarly stick skating movies
Advertisement
Quote:Original post by crazy_andy
How can you write to a file in binary mode, i worked out the following code, but it doesnt work:


FILE *file2 = fopen(filename,"wb");
write("0",1,1,file2);

This code just writes 0 to the file I want to create, rather than the binary code 0.


Try this:

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>int main(){  int data = 42;  int file = creat("filename", S_IREAD | S_IWRITE);  write(file, &data, sizeof(int));  close(file);}


Quote:Original post by crazy_andy
This code just writes 0 to the file I want to create, rather than the binary code 0.


I may be completely wrong here but you are writing one byte '0' to the file if you open it up in notepad that's exactly what you will see. So your code actually works and writes one byte '0' to the file. [grin]


[Edited by - NullPointer33 on August 16, 2004 5:16:19 AM]
Or this:


// savevoid CPlayer::savePlayerFile(const char*  playerfile){    FILE *pfile;   pfile = fopen(playerfile, "wb");        fwrite(&var1_short, sizeof(short), 1, pfile);     fwrite(&var2_int, sizeof(int), 1, pfile);   fclose(pfile);     return true;}


// loadvoid CPlayer::loadPlayerFile(const char *playerfile){  FILE *pfile;     pfile = fopen(playerfile, "rb");                  fread(&var1_short, sizeof(short), 1, pfile);       fread(&var2_int, sizeof(int), 1, pfile);        fclose(pfile);     return true;}




::edit:: ok sorry that wasn't what you wanted
Your codes looks right.
Saving a string or characters to a file in binary mode will
look very much the same as if you did it in text mode. The only
difference will be that the binary file will contain a ascii character '0' denoting the end of string if you save a string to file.
If you want a more convincing test, save a numeric value to a file
rather than a string or character.
'0' is also bad, it's still the ASCII code. That is, '0'!=0; instead '0'=="0"[0]. If you want a 0, write 0 or '\0'.

However, note that, you can usually only write to a file in bytes, not bits. So even if you write(0,1,1,file), you're really writing eight 0 bits. I don't think there's any easy way to write a single bit, since memory is organized in bytes and all the read/write commands use bytes.

The best thing to do is not to worry about writing binary. Instead, write your ints, floats, structs, etc directly, as shown in the examples others posted. This just "dumps" the variables as they are in memory, so you don't have to worry about the format.
The reason I want to write in binary, is because I want to write a .TGA file, and I know what each byte does, I assumed that since we read from a .TGA in binary, I could make one using binary.

However if you open a .TGA in notepad, you see a load of weird characters, so I would need to write binary.. I think I am doing the correct thing, but if anyone knows a better way, please point mein the right direction.
www.stickskate.com -> check it out, some gnarly stick skating movies
by the way I am using c++
www.stickskate.com -> check it out, some gnarly stick skating movies
Quote:Original post by crazy_andy
The reason I want to write in binary, is because I want to write a .TGA file, ...

For what reason?
I know very little about this sort of thing, but there might be a function (in an image manipulation library or something) that will create the .TGA for you from an HDC or something.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Writing "0" as a character is not a good way to write binary to file... If I understand you right, you wanted to write something like "00010001" to the file, hoping it will "generate" appropriate byte... what you NEED to do is to write whole structures and read them also... eg. header of the TGA consists of some parts and thus, you need to create structure of the header and you have to write / read it to / from the file as a whole and then read the values. This will do the trick of writing binary values to it... and then the image data is read by bytes,... or just what fits you best... hope you've understood...
m.

This topic is closed to new replies.

Advertisement