• C

passing data problems

i'm having trouble trying to get data written into a one-field structure called 'JOBINDEX'.  this index file will give me the next job number when the user tries to enter a new job.  after getting the next job number from 'JOBINDEX' file, it should write the whole record into a 'JOBMSTR' file and also write the next(current) job number into 'JOBINDEX' file for the next entry.  i couldn't get the data to write and save into the 'JOBINDEX' file and whenever i try to read , i get the old record (it doesn't update).  below is the structure, a part of my code from main, and the passing structure code: - i'm compiling these in UNIX OpenServer 5.
/****************************************************/
/*         structure    
/****************************************************/
typedef struct
{
    int jobno;
} JOBINDEX;

typedef struct
{
   int jobno;
   char whse;
   char stat;
   char custno;
   char custname;
} JOBMSTR;
/********************************************************/
/****      part of the main file..
/******************************************************
add_jobmstr()
{
      int x, numread, result, m, numwrite;

       char string[80], bufwrite[80];

      char tmp_emplno[5], tmp_job[6];

      char tempkey[4], tmpblk1[9];

      EMPLOYEE num;
      JOBMSTR jbmstr;
      JOBDTL jbdtl;
         PYRLREC rec;

      JOBINDEX idx;

      long pos = 0;

      int nextjob;

      int curjob;


/* ------------------------------------------- DEFINE ------*/
      unsigned long i, count;


/* ---------------------------------------------- GET DATA --*/
         fflush(stdin);
      if(get_newjob(&idx))
      {
            printf("Got the new job no.\n");
            printf("read idx: %d\n",idx.jobno);
            
            

            nextjob = idx.jobno + 1;
            jbmstr.jobno = nextjob;


               printf("Enter Job Whse: ");

              gets(jbmstr.whse);
      printf("Enter Job Customer NO: ");
            gets(jbmstr.custno);
                   printf("Enter Job Customer Name: ");
                    gets(jbmstr.custname);
      jbmstr.stat[0] = 'A';

                 if(add_jobmstrrecord(nextjob,&jbmstr))
      {
             printf("\n RECORD APPENDED INTO EMPLOYEE FILE\n");
         printf("\nafter add jobno: %d",jbmstr.jobno);
                  printf("\nafter add custname: %s",jbmstr.custname);
                  printf("\nafter add whse: %s",jbmstr.whse);
            }
            else
            printf("\nERROR IN UPDATING JOBMSTR\n");


            if (update_idxrec(nextjob,&idx))
            {
                  printf("\nINDEX FILE UPDATED\n");
            }
            else
                  printf("\nERROR UPDATING INDEX FILE\n");
          }
      else
            printf("ERROR getting new job      \n");


   return;

}/*end */

/********************************************************
/*      HERE'S WHERE THE FUNCTION POINTS TO
/****************************************************
int add_jbmstrrecord( int jobnostr,JOBMSTR *jbmstrrec)
{
    int returnval = 0
    if ((fp = fopen("jobmstr.dat","r+"))==NULL)
      printf("ERROR");
    else
    {
       returnval = 1;
       while(!feof(fp))
       {
         fread(&jbmstr,sizeof(JOBMSTR),1,fp);
         if(jbmstr.jobno == jobnostr)
         {
            printf("REcord already exist");
            returnval = 0;
         }
      }
      if (returnval == 1)
      {
         if(fwrite(jbmstrrec, sizeof(JOBMSTR),1,fp) !=1)
           printf("Error");
      }
      fclose(fp);
   }
   return returnval;
}

int update_idxrec(int nxtjob, JOBINDEX *index)
{
   int returnval = 0;
   if ((fp = fopen("jobidx.dat","r+")) == NULL)
      printf("ERROR");
   else
   {
     idx.jobno = nxtjob;
     if(fwrite(index,sizeof(JOBINDEX),1,fp) != 1)
        printf("ERROR");
     returnval = 1;
    }
    fclose (fp);
    return returnval;
}
zandy1Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkrConnect With a Mentor Commented:
That seems quite easy... you'll have to open the file with read/write access (well, 'write' is more important):

    if ((fp = fopen("jobmstr.dat","rw+"))==NULL)


   if ((fp = fopen("jobidx.dat","rw+")) == NULL)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.