Solved

passing data problems

Posted on 1998-11-10
1
273 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 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

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

749 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