Re: [NEWBIE][CODE] Files

From: Peter Ajamian (
Date: 03/25/01

Its Me Christian wrote:
> eg. if the file were to be saved like this:
> CLANNAME: clannamehere
> LEADERNM: leadernamehere
> so i want a varable (GET_CLAN_NAME(ch)) to equal clannamehere. could someone
> please help me with that.

I'm assuming that you already have code to read a line of input from the
file so I won't go into that, I'll give you a general overview of the

First you need to parse the input line, there are a few different
functions available that can help you do it, my personal choice is
strtok() though if you use that you need to be aware that it trashes the
input line so if you want to preserve the input you need to first copy it
with strcpy().  The function is a bit complex, but it basically works
like this:

ptr = strtok(buf, ": ");

That will first skip any leading characters in buf that match any
characters in the second arg (: or space), then it will copy the address
of the first non-matching character to ptr.  It will then find the next
instance of any of the characters in the second arg and change that to
null.  You end up with ptr containing the first part of the parsed
string, to get the rest you do something like this...

ptr2 = strtok(NULL, "\n");

By passing NULL as the first arg to strtok(), you are telling it to start
back up where it left off last time.  It will basically return the rest
of the line (while effectively chopping off the \n at the end for you).

After parsing the input you need to use an if/else if ladder type
construct to test against each possible different tag that could be in
the file.  Something like this...

if (!strcmp(ptr, "CLANNAME")) {
  /* Do this */
} else if (!strcmp(ptr, "LEADERNM")) {
  /* Do that */
} else if ...


} else {
  /* No match, send appropriate error message. */

Good luck :)

Regards, Peter

   | FAQ: |
   | Archives: |

This archive was generated by hypermail 2b30 : 12/04/01 PST