Custom file formats

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Custom file formats

Post by wtetzner »

avansc wrote:as far as i know you still cant write entire objects to it... i may be wrong. but i think a self made binary file would still be beter in terms of that.
Hmm? It has a blob data type (basically you just dump any binary data to it). Also, most object structures can be mapped to a relational model, so that shouldn't be a problem. Anyway, it probably isn't that appealing to people that don't have much relational database experience. Just thought I'd add it to the topic as another possibility.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Custom file formats

Post by dandymcgee »

avansc wrote: as far as i know you still cant write entire objects to it... i may be wrong. but i think a self made binary file would still be beter in terms of that.
Do you have a simple example of how you would write an entire object to a binary file? I think I know what you mean but I'm not exactly sure.
Last edited by dandymcgee on Sat Mar 07, 2009 6:21 pm, edited 1 time in total.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Custom file formats

Post by avansc »

dandymcgee wrote:
avansc wrote: as far as i know you still cant write entire objects to it... i may be wrong. but i think a self made binary file would still be beter in terms of that.
Do have a simple example of how you would write an entire object to a binary file? I think I know what you mean but I'm not exactly sure.

here is something. note that this is just what i made in 5 min. you can do pointers but there are ways of gettign around it.
anyways. as you can see its pretty nifty. ps: just make sure you delete the file manually, i just noticed i didnt put that in there.

Code: Select all

#include <stdio.h>
#include <stdlib.h>

#include <fstream.h>

using namespace std;

class map
{
    public:
        map();
        void fill();
        void printData();
    private:
        int data[10][10];
};

map::map()
{
}

void map::fill()
{
    for(int a = 0;a < 10;a++)
    {
        for(int b = 0;b < 10;b++)
        {
            this->data[a][b] = (a+1)*(b+1);
        }
    }
}

void map::printData()
{
    for(int a = 0;a < 10;a++)
    {
        for(int b = 0;b < 10;b++)
        {
            printf("%d , ",data[a][b]);
        }
        printf("\n");
    }
}

void saveMap(char *fileName, map *temp);
map *loadMap(char *fileName);

void saveMap(char *fileName, map *temp)
{
    ofstream myFile(fileName, ios::out | ios::binary);
    myFile.write((char*)temp, sizeof(map));
    myFile.close();
}

map *loadMap(char *fileName)
{
    map *temp = (map*)malloc(sizeof(map));

    fstream myFile(fileName, ios::binary | ios::in);
    myFile.read((char*)temp, sizeof(map));
    myFile.close();

    return temp;
}

int main()
{
    printf("start of program.\n");

    map *temp = new map();
    temp->fill();
    temp->printData();
    saveMap("lev.bin", temp);

    printf("\n");

    map *lmap = (map*)malloc(sizeof(map));
    lmap = loadMap("lev.bin");
    lmap->printData();

    return 0;
}

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Cole S.
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Sat Dec 06, 2008 2:49 pm

Re: Custom file formats

Post by Cole S. »

so, i can either create a binary file or create a txt file holding information which i can load through C++ classes/functions, binary files are not human readable so it is already encrypted and i would have to create an editor for it, if i use a txt file, it is human readable, but is there a way to encrypt it and have it non-readable? i'm not saying i will do that, but that is what some professional companies do, and i might keep it in mind for later.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Custom file formats

Post by Ginto8 »

you could also overload the insertion operator:

Code: Select all

class Class
{
    int i, j;
    friend ostream &operator<<(ostream &stream, Class classVar);

    public:
       Class(int I, int J)
       {
           i = I;
           j = J;
       }
};

ostream &operator<<(ostream &stream, Class classVar)
{
    stream << classVar.i << " " << classVar.j << endl;
    return stream;
}
Just another way to do it. ;) :mrgreen:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Custom file formats

Post by avansc »

GOD i dont like that.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: Custom file formats

Post by CC Ricers »

avansc wrote:wht do so few people utilize binary files?
I would like to use binary files. Not specifically to edit for the reasons Cole S. gave, but as a final output for a file that will be used with the program. I've made my own .obj model loader that parses the text in the .obj file. Problem is, these files load somewhat slow (3 seconds for 10 models around 1,000 polygons each) and the text format makes them large. I'd make a file converter to turn the text into raw numerical data, as most of it is numbers anyways.
Post Reply