Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Updating a random access file

Avatar of kgpretty
kgpretty asked on
C
16 Comments1 Solution2206 ViewsLast Modified:
Below is the program code I have been working on.
See the structure called Tool below.
In the update function, I'd like to have the user enter an idNumber of a tool
and then have them enter a name which corresponds to the id number.
The update function should then update the name field of the record.
So far, I've been unable to do this. What is wrong with the update function?



// Kimberley M. Griffith (406003213)
// March 24th 2007

#include <stdio.h>
#include <ctype.h>

typedef struct
{
      int idNumber;
      int quantity;
      double cost;
      char name[20];
}Tool;


void transfer();
void update();
void check();


int main()//serves as menu
{
      //Initialise variables
      int selection;

      /* POPULATES THE OLDHARDWARE FILE FOR PURPOSES OF THE PROGRAM
      FILE* odPtr; //fPtr is data file pointer

      Tool oldData[4] =
      {
            {5, 7, 57.98, "Electronic sander"},
            {9, 76, 11.99, "Hammer"},
            {11, 21, 11.00, "Jig saw"},
            {2, 0, 79.50, "Lawn mower"}
      };

      odPtr = fopen("oldhardware.dat", "w");
      if(odPtr == NULL)
      {
            printf("\nThe file you entered cannot be open. \nPlease try again.\n"); //exit or return
      }
      else
      {
            //filling in the records
            for(int i = 0; i < 4; i++)
            {
                  fwrite(&oldData[i], sizeof(Tool), 1, odPtr);
                  fflush(stdin);
            }
      }
      fclose(odPtr);
      */

      do
      {
            selection = NULL;

            printf("\nMenu List");
            printf("\n[0] Exit the program");
        printf("\n[1] Transfer information from an old to a new file");
            printf("\n[2] Update the records");
            printf("\n[3] Add a new tool");
            printf("\n[4] Delete a tool");
            printf("\n[5] Display record information");
            printf("\nEnter your selection here:");
            scanf("%u", &selection);


            switch(selection)
            {
                  case 0: return 0;
                  case 1: printf("\nTransfer information from an old to a new file");
                              transfer();
                              check();
                        break;
                  case 2: printf("\nUpdate the records");
                              update();
                              check();
                        break;
                  case 3: printf("\nAdd a new tool");
                        break;
                  case 4: printf("\nDelete a tool");
                        break;
                  case 5: printf("\nDisplay record information");
                        break;
                  default: printf("The value you entered is invalid.");
            }//end of switch statement


      }
      while(selection > 0 || selection < 5);
      
      return 0;
}

void transfer()
{
      FILE* odPtr;
      FILE* ndPtr; //fPtr is data file pointer

      Tool oldData[4];

      odPtr = fopen("oldhardware.dat", "r");
      if(odPtr == NULL)
      {
            printf("\nThe file you entered cannot be open. \nPlease try again.\n"); //exit or return
      }
      else
      {            
            ndPtr = fopen("newhardware.dat", "w");
            if(ndPtr == NULL)
            {
                  printf("\nThe file you entered cannot be open. \nPlease try again.\n"); //exit or return
            }
            else
            {
                   for(int i = 0; i < 4; i++)
                  {
                        fread(&oldData[i], sizeof(Tool), 1, odPtr);
                        fwrite(&oldData[i], sizeof(Tool), 1, ndPtr);
                  }
            }
            fclose(ndPtr);
      }
      fclose(ndPtr);
}//end transfer function


void check()
{
      FILE* checkPtr; //fPtr is data file pointer
      Tool checkData[4];

      checkPtr = fopen("newhardware.dat", "r");
      if(checkPtr == NULL)
      {
            printf("\nThe file you entered cannot be open. \nPlease try again.\n"); //exit or return
      }
      else
      {
            //filling in the records
            for(int i = 0; i < 4; i++)
            {
                  fread(&checkData[i], sizeof(Tool), 1, checkPtr);
                  printf("\n%u, %u,%.2lf,  %s\n", checkData[i].idNumber, checkData[i].quantity, checkData[i].cost, checkData[i].name);
            }
      }
      fclose(checkPtr);
}//end check function


void update()
{
      FILE* ndPtr; //fPtr is data file pointer

      int idNum;
      //char choice;

      Tool updateData;
      
            /*
      printf("\nNow, enter a letter from the menu below to tell us what you would like to edit");
      printf("\nA. Name");
      printf("\nB. Quantity");
      printf("\nC. Cost");
            scanf("%c", &choice);
            
      choice = toupper(choice);

      switch(choice)
            {
                  case 'A':
                        break;
                  case 'B':
                        break;
                  case 'C': printf("\nDisplay record information");
                        break;
                  default: printf("The value you entered is invalid.");
            }//end of switch statement
            */
            bool change = false;

      ndPtr = fopen("newhardware.dat", "a+");
      if(ndPtr == NULL)
      {
            printf("\nThe file you entered cannot be open. \nPlease try again.\n"); //exit or return
      }
      else
      {      
            
            printf("Enter the record number of a tool to edit:");
            fflush(stdin);
            scanf("%u", &idNum);

            fseek(ndPtr, (idNum-1)*sizeof(Tool),SEEK_SET);
            fread(&updateData, sizeof(Tool), 1, ndPtr);

            //change = true;
            printf("\nupdate data structure:\n%u, %u,%.2lf,  %s\n", updateData.idNumber, updateData.quantity, updateData.cost, updateData.name);

            printf("\nEnter the name to change to:");
            fflush(stdin);
                  scanf("%s", updateData.name);

            fseek(ndPtr, (idNum-1)*sizeof(Tool),SEEK_SET);

            //printf("\n%u, %u,%.2lf,  %s\n", updateData.idNumber, updateData.quantity, updateData.cost, updateData.name);

            //resets pointer to the start of the record to be updated
            //fseek(ndPtr, (idNum-1)*sizeof(Tool),SEEK_SET);
            fwrite(&updateData, sizeof(Tool), 1, ndPtr);
            //}
      }
      fclose(ndPtr);

}//end transfer function
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. DominicusFlag of Germany imageCEO/Programmer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answers