October 06, 2011
Hi, its been a while since ive done any programming, and ive been stuck on the
below program
for too long now...its driving me nuts! lol

Aim of program is to update simple2.bin with the contents of adv-header.bin,
and then output the
updated file as simple3.bin.

(The program compiles with no errors warnings)

Problem: Once the program is complete the ouput simple3 has not been updated!
Yet if I follow the code in the debugger, I can see the master file
(advmem_ptr which
is a pointer constant passed to malloc) is being updated using the pointer
adv_ptr which
is pointing to advmem_ptr.   I just cant see the wood for the trees.  Any help
is very much
appreciated.

Paul

----------------------------------------------------------------------------------------------

/* Program to marry-up ADV Header with Simple data */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <adv-eq.h>


void mem_error(char * file_name);
void read_error(char * file_name);
void move_error(char * file_name);
struct advhdr * advhdr_alloc(struct advhdr *advmem_ptr, long *advsize_ptr,
                              FILE *adv_fp);
struct simple * simpleform_alloc(struct simple *runmem_ptr, long *simplesize_ptr,
       FILE *simpleform_fp);



int main()
{

   FILE *adv_fp,
        *simpleform_fp,
        *updated_fp;



   struct advhdr adv, *advmem_ptr, *adv_ptr;
   struct simple simple, *simplemem_ptr, *simple_ptr;

   long *advsize_ptr,
         advsize,
        *simplesize_ptr,
         simplesize,
         race_ctr = 0,
         simple_ctr = 0;


   /* open files */

   adv_fp           = open_file("adv-header.bin","rb");
   simpleform_fp    = open_file("simple2.bin","rb");
   updated_fp       = open_file("simple3.bin","wb");



   /* Assign pointers for size of file to be used in malloc */
   advsize_ptr    = &advsize;
   simplesize_ptr = &simplesize;

   /* Alloc memory */
   advmem_ptr      = advhdr_alloc(advmem_ptr,advsize_ptr,adv_fp);
   simplemem_ptr   = simpleform_alloc(simplemem_ptr,simplesize_ptr,simpleform_fp);

   /* Assign pointers */
   adv_ptr   = advmem_ptr;
   simple_ptr = simplemem_ptr;

   while( race_ctr < advsize )
   {


         while(simple_ctr < simplesize )
         {



            if( (!strcmp(simple_ptr->course,adv_ptr->course)) &&
(!strcmp(simple_ptr->date,adv_ptr->date)) &&
(!strcmp(simple_ptr->race_time,adv_ptr->race_time)))
            {
               simple_ptr->a_ave_rating  = adv_ptr->a_ave_rating;
               simple_ptr->win_margin    = adv_ptr->win_margin;
               simple_ptr->place_margin  = adv_ptr->place_margin;
               simple_ptr->book_1        = adv_ptr->book_1;
               simple_ptr->neg_rated     = adv_ptr->neg_rated;
               simple_ptr->a_bias        = adv_ptr->a_bias;
               simple_ptr->book_2        = adv_ptr->book_2;
               simple_ptr->b_ave_rating  = adv_ptr->b_ave_rating;
               simple_ptr->b_bias        = adv_ptr->b_bias;
            }
            simple_ptr++;
            simple_ctr++;


        } /* end while */




      simple_ptr = simplemem_ptr;  /* start of array */
      simple_ctr = 0;
      adv_ptr++;
      race_ctr++;
   } /* end outer while */



   if( fwrite(simplemem_ptr,simplesize * sizeof(struct simple),1,updated_fp) != 1)
   {
      printf("Error writing to file...Exiting");
      exit(1);
   }

   printf("Programme Successful");
   getch();

   fclose(adv_fp);
   fclose(simpleform_fp);
   fclose(updated_fp);


} /* end main */



struct advhdr * advhdr_alloc(struct advhdr *advmem_ptr, long *advsize_ptr,
       FILE *adv_fp)
{
  long file_size;

  if( (fseek(adv_fp,(long) 0 ,SEEK_END)) != 0)
  {
    move_error("runners file");
  }

  file_size = ftell(adv_fp)/sizeof(struct advhdr);
  rewind(adv_fp);

  advmem_ptr = (struct advhdr *) malloc(file_size * sizeof(struct advhdr));

  if( advmem_ptr == NULL)
       mem_error("advhdr file");

  if( fread(advmem_ptr,sizeof(struct advhdr),file_size,adv_fp) != file_size)
     read_error("advhdr file");

  *advsize_ptr = file_size;

  return advmem_ptr;
}

struct simple * simpleform_alloc(struct simple *simplemem_ptr, long
*simplesize_ptr,
       FILE *simpleform_fp)
{
  long file_size;

  if( (fseek(simpleform_fp,(long) 0 ,SEEK_END)) != 0)
  {
    move_error("simpleform file");
  }

  file_size = ftell(simpleform_fp)/sizeof(struct simple);
  rewind(simpleform_fp);

  simplemem_ptr = (struct simple *) malloc(file_size * sizeof(struct simple));

  if( simplemem_ptr == NULL)
       mem_error("simpleform file");

  if( fread(simplemem_ptr,sizeof(struct simple),file_size,simpleform_fp) !=
file_size)
     read_error("simpleform file");

  *simplesize_ptr = file_size;

  return simplemem_ptr;
}



 void mem_error(char * file_name)
{
   printf("Memory allocation error %s ...Exiting",file_name);
   exit(1);
}

void read_error(char * file_name)
{
   printf("File reading error %s...Exiting",file_name);
   exit(1);
}

void  move_error(char * file_name)
{
   printf("FSeek error in file %s...Exiting",file_name);
   exit(1);
}