View Full Version: fread_factory

C++ Learning Community > Other Programing Languages > fread_factory


Title: fread_factory
Description: EOF problems in C


feldmarschall - December 5, 2006 07:30 PM (GMT)
Im totally flustered and pissed that this is just not working.

Alright im trying to work on this code to read information from a file and then create a linked list. This is in C.

Basically what you have to know is FACTORY_DATA is a structure, and the CREATE is a memory macro with LINK linking the previous item of the list to the next. fread_word and fread_number are functions that work and read a word and a number obviously.

The problem is when I try to load I get a screen full of errors saying

fread_word: EOF Encountered. Ocassionally I get the fread_word bad format (L) error.

When clearly it should not be. I've had a lot of these problems. When I change one or two lines it goes back to these errors and I have to restore from backups. Just doesn't make sense. I swear to god if I sneeze it stops working.

My question is why would this be happening? Theoretical reasons are accepted.

Also the chmod for the file is fine.

CODE

void readfactory( void )
{
  char *wordy; /* The temporary string */
  int number; /* Temporary number */
  FACTORY_DATA *factory;

  FILE *fid = NULL;
  fid = fopen( "../system/factory.dat", "r" );
  if( !fid )
  {
     /*
      * send_to_char("Didnt open. \n\r",ch);
      */
     return;
  }

  int i;
  int q;

  for( i = 1; !feof( fid ); i++ )
  {


      CREATE( factory, FACTORY_DATA, 1 );
     LINK( factory, first_factory, last_factory, next, prev );

     wordy = fread_word( fid );


     wordy = fread_word( fid );



     factory->name = STRALLOC(wordy);



     wordy = fread_word( fid );
     wordy = fread_word( fid );



     factory->manager = STRALLOC(wordy);

     wordy = fread_word( fid );
     number = fread_number( fid );

     factory->level = number;

     wordy = fread_word( fid );
     number = fread_number( fid );

     factory->value = number;

     wordy = fread_word( fid );
     number = fread_number( fid );

     factory->contractvalue = number;

     wordy = fread_word( fid );
     number = fread_number( fid );

     factory->pccontract = number;

     wordy = fread_word( fid );
     number = fread_number( fid );

     factory->wage = number;



     wordy = fread_word( fid );
     number = fread_number( fid );

     factory->vnum = number;

     for( q = 0; q < NUM_RAW; q++ )
     {
        wordy = fread_word( fid );
        number = fread_number( fid );
        factory->rawmaterial[q] = number;
     }

     for( q = 0; q < NUM_RAW; q++ )
     {
        wordy = fread_word( fid );
        number = fread_number( fid );
        factory->maxraw[q] = number;
     }

     wordy = fread_word( fid);
     number = fread_number (fid);

     factory->stock = number;

     wordy = fread_word( fid );
     number = fread_number( fid );

     factory->state = number;

     wordy = fread_word( fid );  /* To read the END entry */
  }
  fclose( fid );



}

myork - December 5, 2006 07:47 PM (GMT)
The problem is caused because the file: "../system/factory.dat" is not in the format you expected.

Without the code for fread_word() it is impossible to tell what is wrong.
But I would bet that the function tries to read a word and fails because it hits end of file.

In the code below you only check at the beginning of the loop.
What happens if you hit the end of file half way through the loop?

You don't check you just keep trying to read stuff. This will cause repeated failures each time you call fread_word().

Each call that may result in an EOF needs to checked.
CODE
     wordy = fread_word( fid );
     if (feof(fid))
     {    /* Something bad happened abort. */
         break;
     }


Note you need to do this for every call to fread_word() (Unless you can guarantee that you will not reach the EOF).

feldmarschall - December 5, 2006 08:23 PM (GMT)
What do you mean isnt in the format I expected?

I added a $ to the end of the file, and added an if statement to break the loop if it finds $. The same thing occured. So as far as I can tell that isnt the problem.

When it reads the info into the game its just random data. So nothing is getting read at all as far as I can tell.

Here is the code to fread_word

CODE

char *fread_word( FILE * fp )
{
  static char word[MAX_INPUT_LENGTH];
  char *pword;
  char cEnd;

  do
  {
     if( feof( fp ) )
     {
        bug( "fread_word: EOF encountered on read.\n\r" );
        if( fBootDb )
           exit( 1 );
        word[0] = '\0';
        return word;
     }
     cEnd = getc( fp );
  }
  while( isspace( cEnd ) );

  if( cEnd == '\'' || cEnd == '"' )
  {
     pword = word;
  }
  else
  {
     word[0] = cEnd;
     pword = word + 1;
     cEnd = ' ';
  }

  for(; pword < word + MAX_INPUT_LENGTH; pword++ )
  {
     if( feof( fp ) )
     {
        bug( "fread_word: EOF encountered on read.\n\r" );
        if( fBootDb )
           exit( 1 );
        *pword = '\0';
        return word;
     }
     *pword = getc( fp );
     if( cEnd == ' ' ? isspace( *pword ) : *pword == cEnd )
     {
        if( cEnd == ' ' )
           ungetc( *pword, fp );
        *pword = '\0';
        return word;
     }
  }

  bug( "Fread_word: word too long" );
  exit( 1 );
  return NULL;
}


Clearly it is hitting the end of the file..but the file has stuff in it to read..its bizarre.

myork - December 5, 2006 08:56 PM (GMT)
QUOTE (feldmarschall @ Dec 5 2006, 03:23 PM)
What do you mean isnt in the format I expected?
Well if it was you would not get any errors.

QUOTE (feldmarschall @ Dec 5 2006, 03:23 PM)
I added a $ to the end of the file, and added an if statement to break the loop if it finds $. The same thing occured. So as far as I can tell that isnt the problem.
That will not help. It will only hit the '$' sign of your file is in the exact specified format. Which it obviously is not.

QUOTE (feldmarschall @ Dec 5 2006, 03:23 PM)
When it reads the info into the game its just random data. So nothing is getting read at all as far as I can tell.
What is more likely that it has read the whole file. You are then trying to create a new record from a file that is now empty and thus your data structure is uninitialised.


QUOTE (feldmarschall @ Dec 5 2006, 03:23 PM)
Here is the code to fread_word
Man no comment on that code. Trying reading up on how to use fscanf().

QUOTE (feldmarschall @ Dec 5 2006, 03:23 PM)
Clearly it is hitting the end of the file..but the file has stuff in it to read..its bizarre.
Not bizarre, it seems obvious to me but you are just blindly ignoring my advice because you think you understand how your code works. Try putting some print statements in to see what is actually happening and try and work out where the first failure is!




Hosted for free by InvisionFree