Top Mud Sites Forum

Top Mud Sites Forum (http://www.topmudsites.com/forums/index.php)
-   MUD Coding (http://www.topmudsites.com/forums/forumdisplay.php?f=9)
-   -   File I/O problems (http://www.topmudsites.com/forums/showthread.php?t=447)

visko 01-12-2003 10:40 PM

I'm restructuring OLC a bit, and in the process I've created a new editor type, which I'm putting into a new .are file. The problem is, I'm getting very odd error messages from bug, to the tune of "Can't find the corrent information from line 0 of your file." This is true because there IS no line 0 of that file. Any reason this code would be looking for line 0 instead of line 1 to start out with? Are there any other things wrong with the code that I can't see because I'm blind?
--------------------------------------------
void load_segments( FILE *fp )
{
SEG_DATA *pSeg;
char *word, line[128];
int i, j;
fgets(line, sizeof(line), fp);
word = fread_word(fp);
if (!str_cmp(word, "Segments"))
i = fread_number(fp);
else
bug("ERROR: Number of Segments not defined.", 0);
for (j = 0; j < i; j++)
{
pSeg = alloc_perm(sizeof(pSeg));
pSeg->vnum = j;
pSeg->name = fread_string(fp);
pSeg->builders = fread_string(fp);
pSeg->seg_flags = fread_number(fp);
pSeg->credit = fread_string(fp);
}
fclose(fp);
}
-----------
I should be adding to this function, especially since this'll end up a linked list, but for now since I only have one segment, wth?
-Visko

jornel 01-13-2003 10:38 AM

I suspect it has something to do with intermixing standard C library file functions fgets - which doesn't increment the mud's line number variable - with mud file library routines fread_xxx which do.


Questions to ask yourself:
- is the .are file actually opened? by routines which initialize the line number?
- why are you fgetsing into a buffer called line, then never using it?

To help you figure out which fread_xxx() call is causing the bug message, insert your own test bug messages before and after the calls to fread_number() so that you can recognize where the program is.

bug("Test: Before the fgets");
bug("Test: Between fgets and fread"):
etc.

After the problem is diagnosed and solved, you remove the test bugs.

Hope this helps.

visko 01-13-2003 07:10 PM


visko 01-13-2003 07:18 PM


jornel 01-13-2003 11:16 PM


visko 01-14-2003 03:07 AM


jornel 01-14-2003 10:12 AM

Glad I could help.

Here's a tip to avoid future woes: have your file opens and file closes in the same higher-level routine, with calls to the section reading routines in the middle. Its cleaner, and you avoid unnecessary (and sometime unexpected) side-effects.

visko 01-16-2003 02:17 AM

Ok, well it reads. But my problems haven't ended. I tried editing the segment I loaded, and it didn't work. On a whim, I threw in a function that listed the current loaded segments. This is what I got:

<1500/1500hp 900/900mv> slist
[Num] [Seg Name ] (Builders) [Credit ]
[1076573508] (null) (null)
[1076573532] (null) @6+@

(Please ignore the bad spacings; I'm cleaing that up later, this was just a hack from alist.)

As you can see, the numbers are a tad wrong (they should start at 1 and move upwards from there...), and the names/builders/credits are just...off. And I don't know why. Does this kind of screwup look familiar to anyone?

Thanks so much, I'll post any other code people want to see to get to the bottom of this.

-Visko

jornel 01-16-2003 08:23 AM

It doesn't look like you are printing your data, you have a bad or missing pointer to your information.  To know more, I'd need to see the line(s) before and upto the line that printed that display.

I also noticed that in your original function you allocate a structure, keep a pointer to it in the variable pSeg, read your strings from the file and then exit.  But no other function can reference this data, since the variable pSeg disappears when load_segments() returns.

visko 01-16-2003 09:40 PM

Right, sorry, I was getting ahead of myself. Let me repost some of the functions, they've changed a bit. There's a few other things to show, as well, I suppose...

void load_segments( FILE *fp )
{
SEG_DATA *pSeg;
char *word, line[128];
int i, j;
fgets(line, sizeof(line), fp);
word = fread_word(fp);
if (!str_cmp(word, "Segments"))
i = fread_number(fp);
else
bug("ERROR: Number of Segments not defined.", 0);
for (j = 1; j <= i; j++)
{
pSeg = alloc_perm(sizeof(pSeg));
pSeg->vnum = j;
pSeg->name = fread_string(fp);
pSeg->builders = fread_string(fp);
pSeg->seg_flags = fread_number(fp);
pSeg->credit = fread_string(fp);
if ( seg_first == NULL )
seg_first = pSeg;
if ( seg_last != NULL )
seg_last->next = pSeg;
seg_last = pSeg;
pSeg->next = NULL;
top_seg++;
}
}

That's the loading function; seg_first and seg_last are globally defined.

The listing function is the following:
void do_slist( CHAR_DATA *ch, char *argument )
{
char buf [ MAX_STRING_LENGTH ];
char result [ MAX_STRING_LENGTH*2 ]; /* May need tweaking. */
SEG_DATA *pSeg;

sprintf( result, "[%3s] [%-27s] (%-8s) [%-10s]\n\r",
"Num", "Seg Name", "Builders", "Credit" );

for ( pSeg = seg_first; pSeg; pSeg = pSeg->next )
{
sprintf( buf, "[%3d] %-29s %-5s %-12s\n\r",
pSeg->vnum,
pSeg->name,
pSeg->builders,
pSeg->credit);
strcat( result, buf );
}
send_to_char( result, ch );
return;
}

Segment structure is the following:

/*
* Segment definition. Work in progress. --Visko
*/
struct segment_data
{
SEG_DATA * next;
char * name;
char * builders;
int seg_flags;
char * credit;
int vnum;
};

Lesse, anything else....

I think that's about it for loading and displaying. If there's anything else you need, or anything obviously wrong, just post and I'll do what I can.

Thanks,
-Visko

jornel 01-16-2003 10:34 PM


visko 01-17-2003 12:46 AM



All times are GMT -4. The time now is 07:47 PM.

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright Top Mud Sites.com 2022