Thread: Database Program

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

    Database Program

    Hi,

    I am currently working on a database program that has a menu, giving the user the following options: Enter data, Edit data, Delete data, Display data, Save data, Load data and Quit.

    At the moment, i have only started working on the enter and display data functions. I've found that entering the data works ok, but the program fails to display this data when selected in the menu. Here is the current code:

    Code:
    /* By Luke Sowersby */
    
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct Details get_details(int);
    void print_details(struct Details);
    int file_save(int, char [], struct Details *);
    int file_load(char [], struct Details *);
    void delete_details (int position,struct Details *fileptr);
    void edit_details (int position,struct Details *fileptr);
    
    struct Details
        {
        char surname[20];
        char forename[20];
        char address[20];
        int telephone;
        char email[20];
        };
    
    int main (void)
    {
    unsigned s;
    struct Details *p;
    int position=0;
    int option;
    
    printf("How many sets of details?\n");
    scanf("%u",&s);
    
    if((p=(struct Details *)malloc(s * sizeof(struct Details)))==NULL)
        {
        printf("Cannot allocate %u bytes",s);
        getch();
        return 1;
        }
    
    do
        {
        printf("\nPlease select one of the following options for the data:\n1. Enter\n2. Edit\n3. Delete\n4. Display\n5. Save\n6. Load\n7. Quit\n");
        scanf("%d",&option);
        switch(option)
            {
            case 1:
                printf("\nS=%d, position=%d",s,position);
                if (position<=(s-1))
                    {
                    get_details(position);
                    position++;
                    }
                else
                    {
                    printf("\nMemory full");
                    }
                break;
            case 2:
                edit_details(position,p);
                break;
            case 3:
                delete_details(position,p);
                break;
            case 4:
                print_details(p[position]);
                break;
            case 5:
                file_save(s,"datafile.txt",p);
                break;
            case 6:
                s=file_load("datafile.txt",p);
                position=s;
                break;
            case 7:
                printf("\nExit");
                break;
            };
        }
    
    while(option!=7);
    
    printf("\n\nHit a key");
    getch();
    free(p);
    return 0;
    }
    
    
    struct Details get_details(int position)
    {
    struct Details get_member;
    
    printf("\nDetails for class member %d",position+1);
    printf("\nEnter the forename of the member: ");
    scanf("%s",get_member.forename);
    printf("\nEnter the surname of the member: ");
    scanf("%s",get_member.surname);
    printf("\nEnter the street address of the member: ");
    scanf("%s",get_member.address);
    printf("\nEnter the telephone number of the birth of the member: ");
    scanf("%d",&get_member.telephone);
    printf("\nEnter the email of the member: ");
    scanf("%s",get_member.email);
    
    return get_member;
    }
    
    void print_details(struct Details print_member)
    {
    printf("\n %s %s %s %d %s",print_member.forename,print_member.surname,print_member.address,print_member.telephone,print_member.email);
    }
    
    int file_save(int size,char filename[],struct Details *fileptr)
    {
    int i;
    FILE *fptr;
    if((fptr=fopen(filename,"wb"))==NULL)
        {
        printf("File cannot be opened\n");
        exit(1);
        return 0;
        }
    fwrite(fileptr,sizeof(struct Details),size,fptr);
    fclose(fptr);
    return 1;
    }
    
    int file_load(char filename[],struct Details *fileptr)
    {
    FILE *fptr;
    int size;
    if((fptr=fopen(filename,"rb"))==NULL)
        {
        printf("File cannot be opened\n");
        exit(1);
        return 0;
        }
    fread(fileptr,sizeof(struct Details),size,fptr);
    fclose(fptr);
    return 1;
    }
    
    void delete_details (int position,struct Details *fileptr)
    {
    int file_load(char filename[],struct Details *fileptr);
    }
    
    void edit_details (int position,struct Details *fileptr)
    {
    int file_save(int size,char filename[],struct Details *fileptr);
    }
    I've highlighted the key areas which i think may be causing this problem.

    Please not, i have not started working with other options yet, and i have only placed the current code in those functions so i could compile it.

    Can anyone provide some help please?

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    position is probably not a valid number when you try to print -- I expect it will be one after all the data you just entered.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    Managed to fix it. The code for case 4 was incorrect. Here is the new code for this:
    Code:
    case 4:
                for (position=0;position<s;position++)
                    {
                    print_details(p[position]);
                    }
    No doubt i'll have further problems later on...

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    I have another issue. Here is my up to date code:
    Code:
    /* By Luke Sowersby */
    
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct Details get_details(int);
    void print_details(struct Details);
    void file_save(int, char [], struct Details *);
    int file_load(char [], struct Details *);
    void delete_details (int position,struct Details *fileptr);
    void edit_details (int position,struct Details *fileptr);
    
    struct Details
        {
        char surname[20];
        char forename[20];
        int telephone;
        char address[20];
        char email[20];
        };
    
    int main (void)
    {
    unsigned s;
    struct Details *p;
    int position=0;
    int option;
    int size;
    
    printf("How many sets of details?\n");
    scanf("%u",&s);
    
    if((p=(struct Details *)malloc(s * sizeof(struct Details)))==NULL)
        {
        printf("Cannot allocate %u bytes",s);
        getch();
        return 1;
        }
    
    do
        {
        printf("\nPlease select one of the following options for the data:\n1. Enter\n2. Edit\n3. Delete\n4. Display\n5. Save\n6. Load\n7. Quit\n");
        scanf("%d",&option);
        switch(option)
            {
            case 1:
                printf("\nSize=%d, position=%d",s,position+1);
                if (position<=(s-1))
                    {
                    p[position]=get_details(position);
                    position++;
                    }
                else
                    {
                    printf("\nMemory full");
                    }
                break;
            case 2:
                edit_details(position,p);
                break;
            case 3:
                delete_details(position,p);
                break;
            case 4:
                for (position=0;position<s;position++)
                    {
                    print_details(p[position]);
                    }
                break;
            case 5:
                file_save(s,"datafile.txt",p);
                break;
            case 6:
                file_load("datafile.txt",p);
                position=size;
                break;
            case 7:
                printf("\nExit");
                break;
            };
        }
    
    while(option!=7);
    
    printf("\n\nHit a key");
    getch();
    free(p);
    return 0;
    }
    
    
    struct Details get_details(int position)
    {
    struct Details get_member;
    
    printf("\nDetails for class member %d\n",position+1);
    printf("\nEnter the forename of the member: ");
    scanf("%s",get_member.forename);
    printf("\nEnter the surname of the member: ");
    scanf("%s",get_member.surname);
    printf("\nEnter the telephone number of the member: ");
    scanf("%d",&get_member.telephone);
    printf("\nEnter the home town/city address of the member: ");
    scanf("%s",get_member.address);
    printf("\nEnter the email of the member: ");
    scanf("%s",get_member.email);
    
    return get_member;
    }
    
    void print_details(struct Details print_member)
    {
    printf("\n %s %s %d %s %s",print_member.forename,print_member.surname,print_member.telephone,print_member.address,print_member.email);
    }
    
    void file_save(int size,char filename[],struct Details *fileptr)
    {
    int i;
    FILE *fptr;
    if((fptr=fopen(filename,"wb"))==NULL)
        {
        printf("File cannot be opened\n");
        exit(1);
        }
    fwrite(fileptr,sizeof(struct Details),size,fptr);
    fclose(fptr);
    }
    
    int file_load(char filename[],struct Details *fileptr)
    {
    FILE *fptr;
    int size;
    if((fptr=fopen(filename,"rb"))==NULL);
        {
        printf("File cannot be opened\n");
        exit(1);
        return 0;
        }
    fread(fileptr,sizeof(struct Details),size,fptr);
    fclose(fptr);
    return size;
    }
    
    void delete_details (int position,struct Details *fileptr)
    {
    int file_load(char filename[],struct Details *fileptr);
    }
    
    void edit_details (int position,struct Details *fileptr)
    {
    int file_save(int size,char filename[],struct Details *fileptr);
    }
    I am yet to start working on the edit and delete functions. The save, print, and edit functions seem to be working fine. However, the program is failing to load the saved file, and "File cannot be opened" is printed on the screen which is the case if the file cannot be opened. Again, i have highlighted the key areas.

    Also, in order to delete a certain person's details, how would i go about this, as i have never dealt with deleting data before?

    Can anyone help with this problem please? Thanks.
    Last edited by lukesowersby; 05-17-2009 at 08:42 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. The problem I see with your file_load is that if you should manage to open the file, you are going to try to read an unknown number of records (possibly negative!) from the file. You have to know how many to read before you start. Do you see a datafile.txt file in your directory when you run the program?

    2. As to deleting data, you have to overwrite the old file with a new file that does not contain the bits you want to delete.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    Quote Originally Posted by tabstop View Post
    1. The problem I see with your file_load is that if you should manage to open the file, you are going to try to read an unknown number of records (possibly negative!) from the file. You have to know how many to read before you start. Do you see a datafile.txt file in your directory when you run the program?

    2. As to deleting data, you have to overwrite the old file with a new file that does not contain the bits you want to delete.
    datafile.txt is in my directory, and updates perfectly well when i choose to save. I'll experiment with it tomorrow. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program for a database
    By cluez in forum C Programming
    Replies: 1
    Last Post: 03-29-2009, 09:36 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  3. very simple database program
    By another_newbie in forum C Programming
    Replies: 6
    Last Post: 05-24-2004, 06:18 PM
  4. Database / Tex
    By petermichaux in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2004, 09:28 PM
  5. Programmers Database
    By sean345 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 09-03-2002, 01:16 AM