?
Solved

passing data problems

Posted on 1998-11-10
1
Medium Priority
?
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 330 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 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