Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, 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.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ Possible File I/O Problem
Possible File I/O Problem
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Pages: 1 2
Posted by
| Trom
(82 posts) Bio
|
Date
| Fri 06 Feb 2004 01:29 AM (UTC) |
Message
| my mud reads in the classes in load_obj_char(), which uses fread_char, fread_obj, fread_pet (fread_class). When it comes to reading the class properties, it works. But when its time to assign the values to the link list in the pfile, it doesn't... I put like log_string (prints to the dos screen) all over the function. It reads the values, assigns them, but when the function is over, it doesn't stick.
Default value for ch->classes->level is -1, after recieving the value from the pfile, it should be "15". The value is retrieved from the pfile, and is assigned to the pointer to the linked list for the ch structure. When the function is done, retrieving information from ch->classes->level shows -1 instead of 15. I've spent a while trying to pinpoint the exact problem, but i can't.
My guess is that it is assigned to the local variable but it doesn't make sense.
this is the function with arguments:
fread_class( CLASS_DATA *class, FILE *fp )
Before the class data is passed off, it finds the last place in the linked list (so its null), then uses that as the argument. fread_class then uses "alloc_mem" so it won't crash the mud, then assigns the values from the pfile. | Top |
|
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Reply #1 on Fri 06 Feb 2004 01:49 AM (UTC) |
Message
| Is it possible that in the end section of the fread_function its defaulting back to -1? or after its being read in, and thats all done, does it get reset in the load_classes section? It might help if you posted the function itself. |
Nobody ever expects the spanish inquisition!
darkwarriors.net:4848
http://darkwarriors.net | Top |
|
Posted by
| Trom
(82 posts) Bio
|
Date
| Reply #2 on Fri 06 Feb 2004 04:52 AM (UTC) Amended on Sat 07 Feb 2004 08:01 PM (UTC) by Nick Gammon
|
Message
|
void fread_class ( CLASS_DATA *cls, FILE * fp )
{
char *word;
//CLASS_DATA *tempclass = ch->classes;
char buf[MSL];
bool fMatch;
CLASS_DATA *class = new_classes( );
int temp = 0;
for ( ;; )
{
word = feof ( fp ) ? "End" : fread_word ( fp );
fMatch = FALSE;
switch ( UPPER ( word[0] ) )
{
case '*':
fMatch = TRUE;
fread_to_eol ( fp );
log_string("fread_char: *");
break;
case 'L':
fMatch = TRUE;
temp = fread_number ( fp );
// test
sprintf(buf, "fread_class, level : %d\n\r", temp);
log_string(buf);
// test
class->level = temp;
// test
sprintf(buf, "fread_class, class->level : %d\n\r", class->level);
log_string(buf);
// test
/*if ( !str_cmp( word, "Level" ) ) {
temp = fread_number ( fp );
sprintf(buf, "%d", temp);
log_string(buf);
tempclass->level = temp < 1 ? 1 : temp;
}*/
break;
case 'I':
fMatch = TRUE;
temp = fread_number ( fp );
// test
sprintf(buf, "fread_class, id : %d\n\r", temp);
log_string(buf);
// test
class->id = temp;
// test
sprintf(buf, "fread_class, class->id : %d\n\r", class->id);
log_string(buf);
// test
/*if ( !str_cmp( word, "ID" ) ) {
temp = fread_number ( fp );
sprintf(buf, "%d", temp);
log_string(buf);
tempclass->id = temp < 0 ? 0 : temp;
}*/
break;
case 'E':
if ( !str_cmp ( word, "End" ) ) {
if (cls == NULL) {
cls = class;
}
else {
cls->next = class;
}
return;
}
break;
}
if ( !fMatch )
{
bug ( "Fread_class: no match.", 0 );
fread_to_eol ( fp );
}
}
return;
}
Edited by Nick to add [code] so we can read it properly.
| Top |
|
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Reply #3 on Fri 06 Feb 2004 05:06 AM (UTC) |
Message
| why not use the KEY macro? cause that always works, as far as I know:
KEY( "Level", class->level, fread_number( fp ) );
Alot more compact, although I realize that your current code if for test purposes. Also, if the second log message, the class->level one if fine, then its getting reset after that somewhere. You may want to read Nick's GDB guide for help something like this. |
Nobody ever expects the spanish inquisition!
darkwarriors.net:4848
http://darkwarriors.net | Top |
|
Posted by
| Trom
(82 posts) Bio
|
Date
| Reply #4 on Fri 06 Feb 2004 05:31 AM (UTC) |
Message
| i expanded it because it does the same thing, i wanted to make sure what was happening. It does assign, but i will have to check out the gdb info thing to see how its being reset, thanks. | Top |
|
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Reply #5 on Fri 06 Feb 2004 08:11 AM (UTC) Amended on Fri 06 Feb 2004 04:43 PM (UTC) by Greven
|
Message
| Something else you might try, grep for class->level ("grep 'class->level' *.c"), and see if at any point it is being reset when it shouldn't be. |
Nobody ever expects the spanish inquisition!
darkwarriors.net:4848
http://darkwarriors.net | Top |
|
Posted by
| Trom
(82 posts) Bio
|
Date
| Reply #6 on Fri 06 Feb 2004 09:54 PM (UTC) |
Message
| done that already and its not, do you happen to have the gdb link? | Top |
|
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Reply #7 on Fri 06 Feb 2004 10:02 PM (UTC) |
Message
| |
Posted by
| Trom
(82 posts) Bio
|
Date
| Reply #8 on Fri 06 Feb 2004 10:30 PM (UTC) |
Message
| I'm still having the same prob, not sure why its resetting. In load_char_obj() it assigns the default values to the class in ch then loads the values from the pfiles. Its acting like its doing that once again at the end. I've checked for resetting using visual c++ 6.0 find in files. | Top |
|
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Reply #9 on Fri 06 Feb 2004 10:34 PM (UTC) |
Message
| These are changes you've done? You could always rip it all out, make sure that everything else works, and then start again. I've had to do this, and its a real pain. However, another option is the GDB stuff, or you might paste what you get from the search for us to see. |
Nobody ever expects the spanish inquisition!
darkwarriors.net:4848
http://darkwarriors.net | Top |
|
Posted by
| Trom
(82 posts) Bio
|
Date
| Reply #10 on Sat 07 Feb 2004 12:14 AM (UTC) |
Message
| grep 'class->level' *.c
act_wiz.c: class->level = level;
music.c: if ( ((class_table[class->id].tier*10+50) > class->level) )
music.c: count += class->level / 5;
music.c: count += class->level / 3;
music.c: count += class->level;
save.c: sprintf(buf, "fread_class, after read loop, class->level : %d", ch->classes->level);
save.c: class->level = temp;
save.c: sprintf(buf, "fread_class, class->level : %d\n\r", class->level);
save.c: tempclass->level = temp < 1 ? 1 : temp;
skills.c: if ( ch->exp < get_exp_req(class_table[cn].tier, class->level+1)) {
skills.c: else if ( class->level >= 100 ) {
skills.c: ch->exp -= get_exp_req(class_table[cn].tier, class->level+1);
skills.c: class->level += 1;
skills.c: if ( ch->exp < get_exp_req(class_table[class->id].tier, class->level) ) {
skills.c: else if ( class->level >= 100 ) {
skills.c: ch->exp -= get_exp_req(class_table[class->id].tier, class->level);
skills.c: class->level += 1;
As for the entire function working, everything seems to load correctly, just not the classes. As in stats affects and objects return on character load up, but the class levels of a char are set to -1, even the character class id's work, level is the main prob. | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #11 on Sat 07 Feb 2004 08:06 PM (UTC) |
Message
| It isn't really clear what your problem is. You are saying when you read the pfile the class is wrong, but you have posted reading the classes, not the pfile.
First thing I would do is check that the classes are read properly, maybe just do a display in the class reading loop.
Next, when loading the *pfile* put a breakpoint around the spot where it loads the pfile, and see what is happening there. Is it assigning a number and then changing it, or assigning -1 in the first place?
Remember in SMAUG there are places where it ignores the level (eg. of objects) and recalculates them based on the area level. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Trom
(82 posts) Bio
|
Date
| Reply #12 on Sun 08 Feb 2004 01:36 AM (UTC) |
Message
| The mud is rot (rom/merc), fread_class(classes, fp), the pointer is reading directly from the pfile. It reads any sections in the pfile that are "#CLASS" then assigns the values to the next empty class in the pfile.
CLASS_DATA *class = ch->classes;
while (class != NULL)
class = class->next;
then class is passed off into fread_class(class, fp). While inside fread_class, the level value is read correctly, and it seems to assign also. But when the function is done reading the class, the value seems to be reset to -1 (which is its initializer in my mud. I don't see in the code were it does this, this is the problem. | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #13 on Sun 08 Feb 2004 02:29 AM (UTC) |
Message
| OK, I see what the problem is. This is it ...
Quote:
if (cls == NULL) {
cls = class;
}
else {
cls->next = class;
}
Why is this a problem? Well, you are passing down a *copy* of the class field (cls) to the function. This is not pass-by-reference.
Let me illustrate. Say I had this:
CLASS_DATA *cls = NULL;
FILE * fp = fopen ("somefile", "r");
fread_class (cls, fp );
// cls is still NULL, no matter what fread_class does
Inside fread_class you have changed "cls" to point to something, but you have changed the *copy* of cls, not the original. Thus, after the function exits cls is still NULL.
Probably the simplest thing is to pass down a pointer to the character (from which you can extract cls), because then if you change ch->classes then that will be OK. You are modifying something *inside* the pointer you are passing down, not the pointer itself.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Trom
(82 posts) Bio
|
Date
| Reply #14 on Mon 09 Feb 2004 06:21 AM (UTC) |
Message
| From my understanding i modified the code like this:
- removed the copy class inside of fread_class()
in load_char_obj(), instead of passing the ch->classes copy of the class variable then assigning memory to it, i did that before and gave fread_class() the resulting NULL class pointer with allocation assigned. Inside fread_class i made all assignments to the cls parameter (got rid of *class variable and made it all *cls).
Somehow i get the same problem. I'm planning to post the code that deals with fread_class in load_char_obj and the fread_class updated. | 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.
58,003 views.
This is page 1, subject is 2 pages long: 1 2
It is now over 60 days since the last post. This thread is closed.
Refresh page
top