Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUDs ➜ General ➜ Read from files

Read from files

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Zeno   USA  (2,871 posts)  Bio
Date Mon 16 Aug 2004 05:16 AM (UTC)

Amended on Mon 16 Aug 2004 05:19 AM (UTC) by Zeno

Message
Eh, I haven't done a lot of code for reading from files, so I ran into a problem. I want to read from a file and store it in the account data. (account->character) But I don't want the character files to be written to, only read. Here's what I have so far:

D_CHAR *load_character(char *player)
{
  FILE *fp;
  D_CHAR *character = NULL;
  char cfile[256];
  char cName[20];
  int size, i;

  cName[0] = toupper(player[0]);
  size = strlen(player);
  for (i = 1; i < size; i++)
    cName[i] = tolower(player[i]);
  cName[i] = '\0';

  sprintf(cfile, "../chars/%s.char", cName);
  if ((fp = fopen(cfile, "r")) == NULL)
    return NULL;

  fclose(fp);
  return character;
}

In the select cmd:

    if ((load_character(arg)) == NULL)
    {
      act_send(act, "No such char.\n\r");
      return;
    }



Everything returns NULL though.

In the main header file, in the account strut:

  D_CHAR        * character;

In the main header file (in diff parts):

typedef struct  character     D_CHAR;

struct character
{
  char          * name;
  char          * skill;
};

D_CHAR *load_character        ( char *character);



So want I want it to do, is when an account selects a character, it loads the char file to account->character, and something like account->character->name will work.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 16 Aug 2004 06:22 AM (UTC)
Message

  sprintf(cfile, "../chars/%s.char", cName);
  if ((fp = fopen(cfile, "r")) == NULL)
    return NULL;

// <-- read it here?

  fclose(fp);
  return character;


You seem to be missing a bit. After opening the file you immediately close it. That would be a good spot to read data from it.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #2 on Mon 16 Aug 2004 06:28 AM (UTC)

Amended on Mon 16 Aug 2004 06:33 AM (UTC) by Zeno

Message
Yeah, I had tried that before, but gave me a crash that wouldn't debug.

  if ((fp = fopen(cfile, "r")) == NULL)
    return NULL;

  word = fread_word(fp);
  while (!done)
  {
    found = FALSE;
    switch (word[0])
    {
      case 'E':
        if (compares(word, "EOF")) {done = TRUE; found = TRUE; break;}
        break;
      case 'N':
        SREAD( "Name",      character->name      );
        break;
      case 'S':
        SREAD( "Skill",  character->skill  );
        break;
    }
    if (!found)
    {
      bug("Load_char: unexpected '%s' in %s's cfile.", word, player);
      return NULL;
    }

    if (!done) word = fread_word(fp);
  }


  fclose(fp);
  return character;


Now the only problem is, how do I define character? That seems to be what I'm having trouble with.

[EDIT] Forgot g3 flag in my Makefile, made I can debug it now.

Yep. Need a way to define character as the file.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Nick Cash   USA  (626 posts)  Bio
Date Reply #3 on Mon 16 Aug 2004 08:00 PM (UTC)
Message
Try, this.

Define a D_CHAR variable,
D_CHAR *char;

Then make the section look like below:

    if ((char = load_character(arg)) == NULL)
    {
      act_send(act, "No such char.\n\r");
      return;
    }
    else
     account->character = char;

Of course since your working with socketmud you'll need to throw in memory allocation, so you need to add something like this in load_character:

    if ((character = malloc(sizeof(*character))) == NULL)
    {
      bug("Load_player: Cannot allocate memory.");
      abort();
    }

Of course you can do something like I did (and SMAUG did) and create memory allocation macros so you dont need to bother with that all the time. Based upon what I've seen, your loading should work, you probably just needed to allocate memory.

~Nick Cash
http://www.nick-cash.com
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #4 on Mon 16 Aug 2004 09:27 PM (UTC)
Message
Thanks, it worked, with a little tweaking. (Wouldn't let me use char as the D_CHAR var, for obvious reasons)

I plan on making a char.lst, and only have the chars in char.lst selectable. How would I do that? Also, I'm going to try to generate a char list from char.lst, but not sure how to do it.

Going to finish up select/char struct for a while, so if I knew a way to do char.lst before I started, it would help.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,997 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.