   Text File Question
  Ok so I wrote a function to convert a couple .obj models into my own model format with animation and some header data to speed loading up.My question is, is there any harm in separating every piece of data with a newline?  I think this would be the fastest because I wouldn't have to compare strings and search for special separating characters while loading the model..obj Example of vertex data:v 231.000000 34.000000 3.000000In my file:231.00000034.0000003.000000Thanks for reading!   
  No harm. It's your format. It won't speed things up or down, there is always the check for the next line (means checking for LF) indirectly.It means cleaner code on your side though, so by all means, go for it.   ;  If you want to speed up loading, maybe just store everything as binary data? That will load way faster than having to parse the text and do a million string to float conversions.   ;  I've heard of storing as binary but it sounds a lot more difficult, not to mention I couldn't find much information on the topic.  If plain text ends up being too slow I'll look into binary but for now, I've got a hundred other things that need to be worked on.  Some simple games just use straight up .obj (or another simplified format) though, I imagine?   ;  Its pretty easy to do. I would be sure if you search for reading and writing binary files in C++ you could find plenty of help. But if your aim is to speed up loading its not likely that shuffling around the text in the file is really going to help too much.   ;Quote:Original post by X Abstract XI've heard of storing as binary but it sounds a lot more difficultIt's not at all difficult.  The binary files just can't be (very easily) read by human eyes.  :)Compare:vector<Vertex> vertices;ifstream fs( "./test.obj" );while ( fs ) {float pos[3];fs >> pos[0] >> pos[1] >> pos[2];vertices.push_back( Vertex( pos[0], pos[1], pos[2] ) );}With:vector<Vertex> vertices;ifstream fs( "./test.bin", ios::binary );while ( fs ) {float pos[3];fs.read( reinterpret_cast<char*>( &pos[0] ), sizeof(pos) ); // Read 3 floats, 4 bytes each into posvertices.push_back( Vertex( pos[0], pos[1], pos[2] ) );}The difference between the two is that the binary file doesn't need to convert strings to floats, and a vertex in a binary file is only 12 bytes in size, whereas a vertex in a plain text file varies, but averages 32 bytes (as per your example).Also, you may need to write a trivial program to convert a plain-text obj file to a binary file.[Edited by - _fastcall on May 7, 2010 1:54:34 AM];  You don't really need information about the binary files.I think it's much simpler to use them than text files: you don't have to do parsing.You know how to write/read data (byte, word, dword) from the file, so all you have to do is to pay attention, that you read/write in the same sequence and the same types.   ;  Ok fair enough, it does look easy :).  Pretty ignorrant question but how do you actually turn data into binary format and save it in a file?edit: Oh nevermind, I have to write something to convert it myself.Quote:Original post by karwostsBut if your aim is to speed up loading its not likely that shuffling around the text in the file is really going to help too much.What I meant was my custom format is faster than .obj because adding header data cuts down on doing an extra pass, and I only store 1 frame worth of faces and tex coords rather than 1 set per frame.   ;Quote:Original post by X Abstract Xhow do you actually turn data into binary format and save it in a file?Easy! [smile]ifstream in( "./input.obj" );ofstream out( "./output.bin", ios::binary );while ( in ) {float pos[3];in >> pos[0] >> pos[1] >> pos[2];out.write( reinterpret_cast<const char*>( &pos[0] ), sizeof(pos) );}It's of course unlikely that you've just got a bunch of vertices in a file; you most likely have a list of faces also:// Helper functiontemplate<typename T>void binaryWrite( ostream& out, const T& in ) {out.write( reinterpret_cast<const char*>( &in ), sizeof(in) );}vector<Vector3> vertices;vector<Face> faces;ifstream in( "./input.obj" );while ( in ) {char type;in >> type;// Todo:  Use getline to enforce error checking// Todo:  Format checkingswitch ( type ) {case 'v': { // vertex: 3 floatsfloat pos[3];in >> pos[0] >> pos[1] >> pos[2];vertices.push_back( Vector3( pos[0], pos[1], pos[2] ) );} break;case 'f': { // face: 3 intsunsigned indices[3];in >> indices[0] >> indices[1] >> indices[2];faces.push_back( Face( indices[0], indices[1], indices[2] ) );} break;}}ofstream out( "./output.bin", ios::binary );// Here is where you can get creative// The method I chose here assumes that the vertices are first in the file// and the number of vertices is known before-hand (in this case, we expect// an integer before we start reading vertices).// Other methods include having a header, specifying the type of the following // block of information (such as 0 for vertices, 1 for faces, etc ), then the// size of the block (sizeof(Vector3)*vertices.size()) to ensure that we don't// read past the end of the block, screwing up consecutive blocks, and etc..// First, the verticesbinaryWrite( out, vertices.size() );for ( unsigned i = 0; i < vertices.size(); ++i );binaryWrite( out, vertices );// Then, the facesbinaryWrite( out, faces.size() );for ( unsigned i = 0; i < faces.size(); ++i );binaryWrite( out, faces );Then you can read like so:// Helper functiontemplate<typename T>void binaryRead( istream& in, T& out ) {in.read( reinterpret_cast<char*>( &out ), sizeof(out) );}vector<Vector3> vertices;vector<Face> faces;ifstream fs( "./output.bin", ios::binary );unsigned size;binaryRead( fs, size );vertices.resize( size );for ( unsigned i = 0; i < size; ++i )binaryRead( fs, vertices );binaryRead( fs, size );faces.resize( size );for ( unsigned i = 0; i < size; ++i )binaryRead( fs, faces );This should give you some ideas on how to get started. [smile][Edited by - _fastcall on May 7, 2010 1:17:14 AM];  Yup, there is no such thing as "turn data into binary format".You write the data as "sadsadsaslkmdkf" type then read it as "sadsadsaslkmdkf" type. They will be stored as sizeof(sadsadsaslkmdkf) bytes of binary data. Your hard drive has no idea about types (my terms are a bit fishy, anyway, I guess you get it now)_fastcall's code does that.   