helppppppppp with mah c database prob plz…the modifying of data in a file aspect

This is a discussion on helppppppppp with mah c database prob plz…the modifying of data in a file aspect within the C Programming forums, part of the General Programming Boards category; I HAVE THIS PROGRAM THAT RUNS CORRECTLY RIGHT ....ITS A DATABASE AND ITS SOPOSE TO BE ABLE TO ADD VIEW ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Unhappy helppppppppp with mah c database prob plz…the modifying of data in a file aspect

    I HAVE THIS PROGRAM THAT RUNS CORRECTLY RIGHT ....ITS A DATABASE AND ITS SOPOSE TO BE ABLE TO ADD VIEW DELETE AND MODIFY A RECORD BUT WHEN I TRY MODIFY A RECORD IN IT THE RECORD IS PLACED AT THE BOTTOM OF THE LIST OF RECORDS AND NOT IN THE SPOT OF THE OLD RECORD MODIFIED IN THE DATABASE..I NEED HELP TO FIGURE OUT HOW TO GET THE MODIFIED RECORD IN THE SPOT IT WAS BEFORE..THIS IS THE CODE
    Code:
    #include <stdio.h>
    #include<string.h>
    #include <conio.h>
    
    struct employee_record{ int id[30],age,yr_joint,r_boats,s_boats;
      char first_name[20];
      char last_name[20];
      char address[20];
      float rsalary;
      float intrest,netpay;};
    
    main()
    { employee_record emp;
      int d_choice;
      int id[30],yr_joint,r_boats,s_boats;
      char doagen,exit;
      char first_name[20];
      char last_name[20];
      char address[20];
      float rsalary=1050.00;
      float intrest,netpay;
    FILE *employee_database,*temp_database,*temp2_database;
    
    printf(“click 1 to Add Records 2 to display Records 3 to Modify Records 4 to delete Records 5 to Exit\n”);
          printf(“Enter your choice\n”);
          scanf(”%d”,&d_choice);
    
          switch(d_choice){
            case 1 :d_choice=1;break;
            case 2 :d_choice=2;break;
            case 3 :d_choice=3;break;
            default: printf(“Invalid option selected\n”);}
    
    if (d_choice==1){
    employee_database = fopen(“C:\\Documents and Settings\\D.D.J.H.S\\Desktop\\NewFolder\\students.txt”,“wb+”);
     
    doagen=‘y’;
    while( doagen== ‘y’){
    printf (“enter i.d\n”);
          scanf(”%d”,&emp;.id);
          printf(“enter employee first name\n”);
          scanf(”%s”,&emp;.first_name);
          printf(“enter employee last name\n”);
          scanf(”%s”,&emp;.last_name);
         
        fwrite(&emp;,sizeof(emp),1,employee_database);
          printf(“Add another Record (Y/N): “);
          fflush(stdin);
          doagen=getchar();}}
    
    else if(d_choice==2)
    {
    
    employee_database = fopen(“C:\\Documents and Settings\\D.D.J.H.S\\Desktop\\New Folder\\students.txt”,“rb+”);
    
    while(fread(&emp;,sizeof(emp),1,employee_database)==1)
    {  printf(”%d “,emp.id);
          printf(”%s\t”,emp.first_name);
          printf(”%s\t”,emp.last_name);}}
    
    else if (d_choice==3)
    {employee_database = fopen(“C:\\Documents and Settings\\D.D.J.H.S\\Desktop\\New Folder\\students.txt”,“rb+”);
      doagen=‘y’;
              while( doagen==‘y’)
              {
                  printf(“Enter the id of the employee to be modified\n”);
                  scanf(”%d”,&id;);
                  temp2_database=fopen(“C:\\Documents and Settings\\D.D.J.H.S\\Desktop\\New Folder\\temp2.txt”,“wb”);
                  while(fread(&emp;,sizeof(emp),1,employee_database)==1)
                  {
                  if(emp.id!=id)
                  {
    
                    fwrite(&emp;,sizeof(emp),1,temp2_database);
                  }
                  }
                    printf (“enter i.d\n”);
                    scanf(”%d”,&emp;.id);
                    printf(“enter employee first name\n”);
                    scanf(”%s”,&emp;.first_name);
                    printf(“enter employee last name\n”);
                    scanf(”%s”,&emp;.last_name);
                    fwrite(&emp;,sizeof(emp),1,temp2_database);
    
                  fclose(employee_database);
                  fclose(temp2_database);
                  remove(“C:\\Documents and Settings\\D.D.J.H.S\\Desktop\\New Folder\\students.txt”);
                  rename(“C:\\Documents and Settings\\D.D.J.H.S\\Desktop\\New Folder\\temp2.txt”,“C:\\Documents and Settings\\D.D.J.H.S\\Desktop\\New Folder\\students.txt”);
                 
                  printf(“modify another Record(Y/N): “);
                  fflush(stdin);
                  doagen=getchar(); }}

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    That's how the while loop is setup. It reads every employee's profile, if it doesn't match the record is written back otherwise the program defers writing
    the changes to the employee profile until the very end.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    yer but is there a way i could get to modify the employee record and it stay in the placement it was before ....instead of it going to the bottom of the list

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    I pointed you to the source of the problem in my earlier post. It lies in the while loop as in:
    Code:
    while(fread(&emp;,sizeof(emp),1,employee_database)==1)
    {
        if(emp.id!=id)
        {
            fwrite(&emp;,sizeof(emp),1,temp2_database);
        }
    }
    go figure!!

    Edit: why the semi-colon (shown in red) in all fread()'s and fwrite()'s
    Last edited by itCbitC; 03-26-2009 at 10:35 PM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    One other small thing I noticed :

    Code:
    printf(“click 1 to Add Records 2 to display Records 3 to Modify Records 4 to delete Records 5 to Exit\n”);
          printf(“Enter your choice\n”);
          scanf(”%d”,&d_choice);
    
          switch(d_choice){
            case 1 :d_choice=1;break;
            case 2 :d_choice=2;break;
            case 3 :d_choice=3;break;
            default: printf(“Invalid option selected\n”);}
    You read in d_choice from the user and then switch on its value. But all you do in the switch is reassign its value. ??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21