Update and search records in files

This is a discussion on Update and search records in files within the C Programming forums, part of the General Programming Boards category; this is a coding for department system. the requirements are to add, update, search and view all records from a ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    Update and search records in files

    this is a coding for department system. the requirements are to add, update, search and view all records from a file. so far i've finished with add and view functions. there's a slight problem with the search function. whenever i try to search for the last record in the file..it will appear twice. anyone know how to solve the problem? and if u can kindly help me with the update functions.



    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void load_menu(void);
    void add(void);
    void update(void);
    void search(void);
    void view(void);
    struct emp e;
    FILE *fp ;
    struct emp
    {
    char id[5];
    char name[100];
    } ;
    
    
    
    
    int main(int argc, char** argv)
    
    
    {
        load_menu();
        return 0;
    }
    
    
    void load_menu(void)
        {
        int choice;
    
    
        do
        {
            printf("\n*****DEPARTMENT SYSTEM*****\n\n");
            printf("1. Add Records\n");
            printf("2. Update Records\n");
            printf("3. Search Records\n");
            printf("4. View Records\n");
            printf("5. Exit\n\n");
            printf("Choose one:\n");
            scanf("%d",&choice);
    
    
            switch(choice)
            {
                case 1: add();
                    break;
                case 2: update();
                    break;
                case 3: search();
                    break;
                    case 4: view();
                    break;
                case 5: printf("Quitting program!\n");
                    system("close");
                    break;
                default: printf("Invalid choice!\n");
            }
    
    
            } while (choice != 5);
            system("cls");
            }
    
    
    void add(void)
            {
    
    
            system("cls");
    
    
            fp = fopen ( "Department System.txt", "a" );
            if( fp == NULL )
            {
                    printf("Cannot open file.\n");
                    printf("Program stopped.\n");
                    system("close");
             }
                    printf("\n*****ADD RECORDS*****\n");
                    printf("\nEnter Department ID : ");
                    scanf("%s",e.id);
                    printf("\nEnter Department Name: ");
                    scanf("%s",e.name);
                    fscanf(fp,"%s %s\n\n",e.id, e.name);
                    fprintf(fp,"%s %s\n\n",e.id, e.name);
                    fclose(fp);
            return;
            }
    
    
    void update(void)
    {
    
    
    }
    
    
    void search(void)
    {
        char sid[5];
            system("cls");
            printf("\n*****SEARCH RECORDS*****\n");
            fp = fopen ("Department System.txt", "r");
            printf("\nEnter Department ID : ");
            scanf("%s",sid);
    
    
            printf("\nID        Name");
            while(!feof(fp)){
            fscanf(fp,"%s %s",&e.id,&e.name);
            if(strcmp(e.id,sid)==0)
            {
                    printf("\n%s      %s ",e.id,e.name);
            }
    }
            printf("\n\n");
            fclose(fp);
    }
    
    
    
    
    
    
    void view (void)
            {
    
    
            system("cls");
            printf("\n*****VIEW RECORDS*****\n");
            fp = fopen ( "Department System.txt", "r" ) ;
            if( fp == NULL )
            {
                    printf("Cannot open file.\n");
                    printf("Program stopped.\n");
                    system("close");
             }
            printf("\nID        Name");
            while ( fscanf ( fp, "%s %s ", e.id, &e.name) != EOF )
            printf ( "\n%s      %s", e.id, e.name);
            printf("\n\n");
            fclose ( fp ) ;
            return;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Code:
    while(!feof(fp)) {
            fscanf(fp,"%s %s",&e.id,&e.name);
    ...
    The EOF-flag is not set when the last element was read successfully; it is set when the first read operation has failed. That's why there is an extra call to fscanf() in your program.

    You may test the return value of fscanf() to control the loop. The return value is the number of items read. ("%s %s" -> 2 items)

    Code:
    while (fscanf(fp, "%s %s", e.id, e.name) == 2) {
    However, you will get a lot of more problems because you have no idea how fscanf() works:

    Code:
    fscanf(fp,"%s %s\n\n",e.id, e.name);
    fscanf ( fp, "%s %s ", e.id, &e.name)
    fscanf(fp,"%s %s",&e.id,&e.name);
    There is only one correct way to call fscanf() here, so two statements are guaranteed to be wrong.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    yeah you're right....it works. thanks. now i just need to do the function for update. do you have any idea how to do that? i've been trying for a few hours.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    4
    here is where i got to but i know it is wrong....i just read from online about it and try it out.


    Code:
    void update(void)
    {
    
    
        char sid[5];
        char ch;
        int nofrec =0;
            system("cls");
            printf("\n*****SEARCH RECORDS*****\n");
            fp = fopen ("Department System.txt", "r");
            printf("\nEnter Department ID : ");
            scanf("%s",sid);
    
    
            printf("\nID        Name");
            while (fscanf(fp, "%s %s", e.id, e.name) == 2) {nofrec++;
            if(strcmp(e.id,sid)==0)
            {
                    printf("\n%s      %s ",e.id,e.name);
            }
    
    
            printf("\nDo you want to update this record? Y or N");
                    scanf("%c",&ch);
                    fseek(fp, ((nofrec-1)*sizeof(e)), 0);
    if(ch=='Y'|| ch=='y')
    {
    printf("Enter the Department ID : ");
    scanf("%s", e.id);
    printf("Enter the Department name: ");
    scanf("%s", e.name);
    fprintf(fp, "id is %s\n", e.id);
    fprintf(fp, "name is %s\n", e.name);
    printf("Record Modified");
    }
    }
            printf("\n\n");
            fclose(fp);
    }

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    4
    anyone can help me with the coding....i need to get it done by today. please help me kindly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Records and Files
    By nuddy in forum C Programming
    Replies: 13
    Last Post: 04-16-2010, 11:29 PM
  2. An imporant question on files and records
    By yashpal in forum C Programming
    Replies: 15
    Last Post: 06-05-2006, 01:33 AM
  3. How to read Records in X-Base files thru C/c++
    By hskambo in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 02:48 AM
  4. Search/Delete records in hash table
    By jpipitone in forum C Programming
    Replies: 3
    Last Post: 03-27-2003, 09:40 PM
  5. Files&Records(Entering records)
    By Beginnerinc in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 08:11 AM

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