Originally Posted by
phantomotap
And you think the only problem with a simply dump of the binary chunk the compiler may hand you in the face of the problems I referenced is supporting arbitrary lengths?
Wow. I didn't mention arbitrary lengths anywhere. That is quite the jumped you've managed.
Not really. Let's look at what you jumped to shall we?
Originally Posted by
phantomotap
Sure... so long as he still manually corrects for the node pointers, doesn't expect that to work across platforms, doesn't add members, doesn't change the order of the members, doesn't add space to the character arrays, and doesn't change the character array to allocate for storage at runtime.
Soma
In little Billy's homework assignment, you have decided that:
1. He needs platform independence.
2. Has decided to reorder his structure after he's written his file.
3. Decided to change the length of all his strings.
4. Threw out all of his saved strings in lieu of making the user enter them every time the program loads instead.
But it is "quite a jump" that I mention arbitrary lengths. Right?
Yes, you do need to pay attention to what you have written, and if you want to change it, load it up and re-save it in the new format. Of course you do. The only time you would ever not need to do this immediately, would be if you instead went through the trouble of saving your data in keyword-data pairs, which is a huge amount of overhead compared to flat-file writing everything. (Which as this example will show, you can add provisions for missing pieces or pieces you no longer need.)
And before you pretend it's not, let me just show you what you are going to involve:
Code:
while( (lineptr = freadline( fp )) != NULL )
{
word = getkeyword( lineptr );
switch( tolower( word[ 0 ] ) )
{
case 'a':
if( strcmp( "aardvark", word ) )
{
myrecord->aardvark = getnumber( lineptr );
if( validrange( myrecord->aardvark, AARDVARKMIN, AARDVARKMAX )
foundaardvark = true;
else
log( "Read error. Got \'%s\': \'%s\'\n", word, lineptr );
}
if( strcmp( "apple", word )
{
...
}
...
case 'b':
...
}
if( !foundaardvark )
myrecord->aardvark = aardvarkdefaultvalue;
if( !foundapple )
myrecord->apple = appledefaultvalue;
...
}
Oh yeah, that's much much better for cases where things will never change once they are finalized!
You don't always need to care about your data being humanly readable, or that fault tolerant, or that flexible. Typically you never to care if "apple" comes in the file before "aardvark", because someone just decided they'd open your save file and swap two lines around, or add more text to a line, just because they felt like it. If someone goes in and mungs up your data, no you don't need to automatically assume it's valid, or test for validity. You don't need this kind of fault tolerance.
Also, if you are in the process of making your application, then yes, you shouldn't be shocked when you decide to change your data around that your old flat file doesn't work pefectly. Why would you expect that? Even in the example I have provided here you still have to go in and manually add a case for "automobile" when you decide to add that field, or to make it ignore "aardvark" if you don't want it anymore. It doesn't magically just appear because I'm no longer using flat files. I don't really know what the point you were trying to make was.
"Hey if you change your data, it will change!
Of course it will! No one is saying that flat files magically fix this. Nothing does! No matter what possible way you can contrive your loading and saving of data, you will always have to load it up, fix it, and write it back out if you do any of the things you have said to look for. NONE of them let you skip this step. You ALWAYS have to fix your data up regardless of how you load or save it. Nothing you have mentioned has some data-safe-magic-bullet that lets you read and write it arbitrarily.
Quzah.