Jump to content
  • Advertisement
Sign in to follow this  
azjerei

Writing to binary

This topic is 4955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I write stuff to a file in binary format? Do I simply do like this?
FILE file = fopen("blah.txt", "wb");
char *line = "This is my line.";
fwrite(line, strlen(line), 1, file);
fclose(file);

Share this post


Link to post
Share on other sites
Advertisement
Writing a string to a file like that becomes difficult to read back in from a binary file.

You usually read/write predefined const sizes of data. Typically using structs.

struct USER_RECORD
{
int user_idl
char user_name[MAX_NAME_LEN];
};

USER_RECORD r;

fwrite( &r, sizeof(USER_RECORD), file );
fread( &r, sizeof(USER_RECORD), file );

or for just a string:

char myName[MAX_NAME_LEN];

fwrite( &myName, sizeof(char) * MAX_NAME_LEN, file );
fread( &myName, sizeof(char) * MAX_NAME_LEN, file );

edit - the reason for this is so you can read in large chunks of data instead of trying to read a file byte by byte and parsing (such as the case of reading null terminated strings)

Share this post


Link to post
Share on other sites
Simplest way is to prefix the string with its length.

FILE file = fopen("blah.txt", "wb");
char *line = "This is my line.";
size_t length = strlen(line);

fwrite( (const char*)&length, sizeof(length), 1, file );

fwrite( line, 1, length, file );

fclose(file);



FILE file = fopen("blah.txt", "rb");
size_t length = 0;
char* line = NULL;

fread( (char*) &length, sizeof(length), 1, file );

line = (char*) malloc(length+1);
fread( line, 1, length, file );
line[length] = '\0';

fclose(file);

Share this post


Link to post
Share on other sites
Ok, but won't that be ascii? I tried adding the "b" indicator to fopen, but it still wrote ascii text I could read.

The thing is that I want to use this for a map format I am doing,and reading ascii has turned out to be damn slow.

Share this post


Link to post
Share on other sites
Quote:
Original post by azjerei
Ok, but won't that be ascii? I tried adding the "b" indicator to fopen, but it still wrote ascii text I could read.


The string data itself *is* binary. There is no difference between text "a" and binary "a". They're both a single byte with value 97.

When you open a binary file with a text editor, every data byte gets interpreted as ASCII (unless you have some sort of unicode-supporting text editor).

All that "writing binary" does is prevent translation of *numbers* into human-readable form.

Share this post


Link to post
Share on other sites
Ok, writing a structure seems to be what I should do. Here is the structure I use for my maps:


struct 3DModel
{
int numObjects;
int numTextures;

vector<Texture> textureInfo;
vector<3DObject> object;
};



I have declared the structure "object"/instance as

3DModel *World;

The textureInfo and object stuff are STL vectors that in themselves use other structs. Would I have to type these out separately somehow? For now, I do this:

(in my write level code)

fwrite(&World, sizeof(3DModel), 1, FilePointer);

(in my read level code)

fread(&World, sizeof(3DModel), 1, FilePointer);

It reads ok, but I see nothing in the level. No faces, no textures, nothing :)

Share this post


Link to post
Share on other sites
What you want to look for is serialization. You could have a look at boost or do a forum search. A simple fwrite with the structure wont work as soon as you have pointers (As used by the stl-containers for example).

Share this post


Link to post
Share on other sites
Well, I wanted to be able to write a map to a file and load it just as easily, preferrably in binary format. I was unsure how to write to binary, though I have come to understand that this won't be an easy task after all.

Share this post


Link to post
Share on other sites
Binary is not that difficult to work with. However, you cannot just dump out complex objects (such as vectors) to binary. This is mainly because they have pointers, and a pointer will be written as just a number (such as 0xFF83 or whatever). And when you go to reload the file, all you will get is a number (such as 0xFF83) which means nothing. The same applies to making assinments between class/structs, a shallow copy versus a deep copy.

So what was suggested was that you have smart classes/structs that know how to write/load themselves to/from a file (or network, etc) - aka serialize

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!