Sign in to follow this  
crazy_andy

How to write to a file in Binary

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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);
}



Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Or this:



// save
void 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;
}







// load
void 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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
'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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by crazy_andy
I'm sorry, but that makes no sense to me, could you give me a quick example


Essentially what he's saying is that if you write "0" to a file you're actually writing the ASCII equivalent of the character 0 to the file and not %00000000 (that's the number 0 in binary).
What you also need to know is exactly how the TGA file format is structured, and the correct file headers (so; bits per pixel, image width/height, if there's an alpha channel or not) in much the same way as an HTML document has a head element containing information like the page title. Except that in an image file you must have all this info.
Hence getting an image library to do it is much easier.

Share this post


Link to post
Share on other sites
I know about the headers, from a nice tut i found on the web, and from lesson 24

I have also worked out how to doit now, i used fwrite and sent a GLubyte variable rather than an actual figure.

thanks alot for the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this