Page 2 of 2
Re: Custom file formats
Posted: Thu Mar 05, 2009 2:13 pm
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.
Re: Custom file formats
Posted: Thu Mar 05, 2009 2:42 pm
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.
Re: Custom file formats
Posted: Thu Mar 05, 2009 4:02 pm
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;
}
Re: Custom file formats
Posted: Thu Mar 05, 2009 4:39 pm
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.
Re: Custom file formats
Posted: Thu Mar 05, 2009 5:22 pm
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.
Re: Custom file formats
Posted: Thu Mar 05, 2009 6:13 pm
by avansc
GOD i dont like that.
Re: Custom file formats
Posted: Thu Mar 05, 2009 9:29 pm
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.