Solved

passing data problems

Posted on 1998-11-10
1
242 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:zandy1
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 110 total points
ID: 1254157
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now