On Sat, 8 Jun 2002, Keith Mervine wrote:
> Greetings,
> How would one do this? I have this writing to a file but it
> prints garbage. Any help is appreciated. Everything but the spell_fun
> writes to a file perfectly.
> fprintf(fp, "%s\n",skill_table[i].spell_fun);
You left out some very relevant code. I'll quote it from my copy of
stock ROM. Merc.h, line 1714, part of the struct skill_type definition.
SPELL_FUN * spell_fun; /* Spell pointer (for spells) */
And on lines 110-111 of stock ROM merc.h we find:
typedef void SPELL_FUN args ( ( int sn, int level, CHAR_DATA * ch,
void * vo, int target ) );
So now we know what data type skill_table[i].spell_fun is: a pointer to
a function. Now we look in our printf reference (man page, C book, or
whatever you prefer to use) and find that the correct format specifier
for printf for a pointer is %p. But you are using %s. Giving printf
the wrong format specifier for your data type is a bad thing.
What compiler are you using? gcc with -Wall should give a warning about
that line when trying to compile, telling you that the format specifier and
the argument data type don't match.
So.. To keep from having garbage printed in your file, use %p instead of
%s when printing out a pointer. Then it will not print garbage. (It still
won't be anything useful, but it won't be garbage).
To get it to work correctly, you'll have to do quite a bit more work.
You'll want to convert the pointer into a string when you print it out,
then back into a pointer when you read it in. I would do this using a
lookup table to convert between function addresses and names.
Dennis