• 12
• 12
• 9
• 10
• 13

Writing to binary

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

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 on other sites
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 );

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 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 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 on other sites
Quote:
 Original post by azjereiOk, 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 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);

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

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 on other sites
didnt he simply want this:

ofstream fout("blah.txt", ios::binary);
fout.write("blah blah", 9);

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.