Solved

passing data problems

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

825 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