Writing to binary

Started by
10 comments, last by Zahlman 19 years, 4 months ago
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);
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)
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);
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
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 :)
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).
didnt he simply want this:

ofstream fout("blah.txt", ios::binary);
fout.write("blah blah", 9);
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.
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

This topic is closed to new replies.

Advertisement