Thread: simple database

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Question simple database

    hi

    this is the code i have to open the following text file:

    rahima,hoque,99133653,19-06-80
    rachael,milor,99159357,29-05-81
    carl,palmer,00214356,29-09-80
    aaron,bimpson,01552336,11-02-80
    louise,irving,00258654,25-03-81
    helen,eckert,00214953,05-07-79
    marion,laverty,98885361,25-05-78
    chris,evans,99543682,04-02-80
    noah,farah,99542364,15-10-78
    lynsay,carmen,01258632,16/12/82

    CODE:

    #include <stdio.h>
    int main()

    {
    FILE * fp;
    int c;
    fp = fopen ("student.txt", "r");
    while (feof(fp)==0)
    {
    c=fgetc (fp);
    putchar (c);
    }
    fclose (fp);
    getchar();
    }

    this code works correctly. How would i allow the user to be able to specify the first record, last record, previous record and next record?

    Thank you

    Rah

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > this code works correctly. How would i allow the user to be able
    > to specify the first record, last record, previous record and next
    > record?

    Hm... This is a tough one. How about staring with a prompt! You could follow that up with actually reading some input from the keyboard! What a 1-2 punch!

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    come on man, help me out here

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What have you tried so far?

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    #include<ctype.h>
    #define SIZE 10
    struct stype {char s1[100], s2[100]; };
    struct stype svar[10];

    typedef struct{
    int day,month,year;
    } struct_date;

    typedef struct{
    char stud_firstname[25];
    char stud_surname[25];
    long stud_id;
    struct_date date_of_birth;
    } struct_stud;
    int comp(const void *a, const void *b){
    struct stype *p1;
    struct stype *p2;
    p1 = (struct stype *)a;
    p2 = (struct stype *)b;
    return strcmp (p1->s2, p2->s2);
    }

    int Main_Menu (void);

    void List_Menu(void);
    void Search_Menu(void);
    void sort_by_id(struct_stud stud_rec[], int n);

    void search_by_id(struct_stud stud_rec[],int n);

    void sort_id(struct_stud stud_rec[],int n);


    void print_record2(struct_stud stud_rec[],int j);
    void print_statement(int tot_rec);
    void swap(struct_stud *stud_rec, int a, int b);

    void convert_to_upper_array(char *ch);
    void convert_to_upper_char(char *ch);
    void check_line_printed(int *line_printed, int max_line);

    void main()

    {
    int choice = 0;
    int i=0;

    while ((choice = Main_Menu())!=2)
    {
    switch (choice)
    {

    case 1:List_Menu();break;
    case 2:exit(1);
    defaultrintf("Invalid selection!\n\n");break;
    }
    }
    } /* main */

    int Main_Menu(void)
    {
    int choice;

    printf("\n\t @ @ @ @ @ @ @ @ @@@@@ @ @ @ @ ");
    printf("\n\t===== @@ @@ @ @ @ @@ @ @@ @@ @ @@ @ @ @ =====");
    printf("\n\t===== @ @ @ @@@@@ @ @ @ @ @ @ @ @@@@ @ @ @ @ @ =====");
    printf("\n\t===== @ @ @ @ @ @ @@ @ @ @ @ @@ @ @ =====");
    printf("\n\t @ @ @ @ @ @ @ @ @ @@@@@ @ @ @@@\n\n\n\n");
    printf("\n\t\t+-----------------------------------------------+");
    printf("\n\t\t| |");
    printf("\n\t\t| |");
    printf("\n\t\t| 1. List student data |");
    printf("\n\t\t| |");
    printf("\n\t\t| 2. Exit |");
    printf("\n\t\t| |");
    printf("\n\t\t+-----------------------------------------------+");
    printf("\n\n\n\n\nPlease enter your choice : ");
    scanf("%d", &choice);

    return choice;

    }

    void List_Menu(void)
    {
    int choice;
    struct_stud stud_rec[10];

    FILE*fp;
    int i=0;

    fp=fopen("M:\\rah.txt","r");

    while(!feof(fp))
    {
    fread(&stud_rec[i], sizeof(struct_stud),1,fp);
    i++;
    }

    fclose (fp);

    do{
    printf("\n\t @ @ @@@@ @@@@@ @@@@ @ @@@@@ @ ");
    printf("\n\t===== @ @ @ @ @ @ @ @ @ @ @ =====");
    printf("\n\t===== @ @ @@@ @ @ @ @@@@@ @ @@@@@ =====");
    printf("\n\t===== @ @ @ @ @ @ @ @ @ @ @ ====="
    printf("\n\t @@@@@ @ @@@@ @ @@@@ @ @ @ @ @ \n");
    printf("\n\t\t+-----------------------------------------------+");
    printf("\n\t\t| 1. Sort by student ID |");
    printf("\n\t\t| |");
    printf("\n\t\t| 2. Back to Main Menu |");
    printf("\n\t\t+-----------------------------------------------+");
    printf("\n\nPlease enter your choice : ");
    scanf("%d", &choice);

    switch (choice)
    {
    case 1:sort_by_id(stud_rec, i-1);break;
    case 2:break;
    defaultrintf("Invalid selection\n");break;
    }
    } while(choice != 2);

    }

    void Search_Menu(void)
    {
    int choice;
    struct_stud stud_rec[10];

    FILE*fp;
    int i=0;
    long id_num = 0;
    fp=fopen("M:\\rah.txt","r");

    while(!feof(fp))
    {
    fread(&stud_rec[i], sizeof(struct_stud),1,fp);
    i++;
    }

    fclose(fp);

    do{
    printf("\n\t\t @@@@ @@@@@ @ @@@@ @@@ @ @ ");
    printf("\n\t\t===== @ @ @ @ @ @ @ @ @ @ =====");
    printf("\n\t\t===== @@@ @@@@ @@@@@ @@@@ @ @@@@@ =====");
    printf("\n\t\t===== @ @ @ @ @ @ @ @ @ @ =====");
    printf("\n\t\t @@@@ @@@@@ @ @ @ @ @@@ @ @ \n\n\n");
    printf("\n\t\t+-----------------------------------------------+");
    printf("\n\t\t| 1. First |");
    printf("\n\t\t| 2. last |");
    printf("\n\t\t| 3. previous |");
    printf("\n\t\t| 4. next |");
    printf("\n\t\t| 5. sort |");
    printf("\n\t\t+-----------------------------------------------+");
    printf("\n\n\n\n\n\nPlease enter your choice : ");
    scanf("%d", &choice);

    switch (choice)
    {
    case 1:search_by_id(stud_rec, i-1);break;
    case 2:break;
    defaultrintf("Invalid selection\n");break;
    }
    } while(choice != 2);

    } /* Searc_Menu */

    void insert_data(void)
    {
    FILE *fp;
    char response;
    char str[10];
    struct_stud stud_rec;

    do{
    printf("Please enter student ID : ");
    scanf("%d", &stud_rec.stud_id);
    printf("Please enter student name : ");
    fflush(stdin);
    gets(stud_rec.stud_firstname);
    convert_to_upper_array(stud_rec.stud_firstname);

    do{
    printf("Enter date of birth (dd/mm/yy) : ");
    fflush(stdin);
    scanf("%2d%[ -/.,\n]%2d%[ -/.,\n]%4d", &stud_rec.date_of_birth.day, str, &stud_rec.date_of_birth.month, str, &stud_rec.date_of_birth.year);
    } while(valid_date(stud_rec.date_of_birth.day, stud_rec.date_of_birth.month, stud_rec.date_of_birth.year));


    fp = fopen("rah.txt", "ab");
    if (fp == NULL) {
    printf("Error in Opening files... Program End\n");
    exit(1);
    }

    fwrite(&stud_rec, sizeof(struct_stud), 1, fp);
    fclose(fp);
    printf("\nDo you want to create another student data (y/n) ?");
    fflush(stdin);
    scanf("%c",&response);
    } while (response=='y' || response=='Y');

    }/* insert_data */

    void sort_by_id(struct_stud stud_rec[], int n)
    {
    int j, line_printed = 0;

    /* Sort all records by ID */
    sort_id(stud_rec, n);



    } /* sort_by_id */

    void sort_by_stud_firstname(struct_stud stud_rec[], int n)
    {
    int j, k, line_printed = 0;

    /* Sort all records by name */
    for(j=0;j<n;j++)
    {
    for(k=n-1;k>j;k--)
    {
    if(strcmp(stud_rec[k].stud_firstname, stud_rec[k-1].stud_firstname)<0)
    swap(stud_rec, k, k-1);
    }
    }

    /* Print all records */
    print_record_header();
    for(j=0;j<n;j++)
    {
    print_record(stud_rec, j);
    line_printed++;
    check_line_printed(&line_printed, 22);
    }

    printf("\nPress any key to continue");
    getch();

    }

    void search_by_id(struct_stud stud_rec[], int n)
    {
    int j;
    long id_num = 0;

    printf("Enter Student ID number : ");
    scanf("%ld", &id_num);

    for(j=0;j<n;j++)
    {
    if (stud_rec[j].stud_id == id_num)
    {
    printf("\nStudent's Record\n");
    printf("----------------\n");
    print_record2(stud_rec, j);
    break;
    }

    if (j==n-1 && stud_rec[j].stud_id != id_num)
    printf("No such record in database\n");
    }

    printf("\nPress any key to continue");
    getch();
    }

    void search_by_stud_firstname(struct_stud stud_rec[], int n)
    {
    int j;
    char firstname[30];

    printf("Enter Student's Name : ");
    fflush(stdin);
    gets(firstname);
    convert_to_upper_array(firstname);

    for(j=0;j<n;j++)
    {
    if (!strcmp(stud_rec[j].stud_firstname, firstname))
    {
    printf("\nStudent's Record\n");
    printf("----------------\n");
    print_record2(stud_rec, j);
    break;
    }

    if (j==n-1 && stud_rec[j].stud_firstname != firstname)
    printf("No such record in database\n");
    }

    printf("\nPress any key to continue");
    getch();
    }

    void sort_id(struct_stud stud_rec[], int n)
    {
    int j, k;

    /* Sort all records by ID */
    for (j=0;j<n;j++)
    {
    for(k=n-1;k>j;k--)
    {
    if(stud_rec[k].stud_id < stud_rec[k-1].stud_id)
    swap(stud_rec, k, k-1);
    }
    }
    } /* sort_id */

    void sort_date(struct_stud stud_rec[], int n)
    {
    int j, k;

    /* Sort all records by date */
    for(j=0;j<n;j++)
    {
    for(k=n-1;k>j;k--)
    {
    if( stud_rec[k].date_of_birth.year < stud_rec[k-1].date_of_birth.year )
    swap(stud_rec, k, k-1 );
    else if(stud_rec[k].date_of_birth.year > stud_rec[k-1].date_of_birth.year );
    else if(stud_rec[k].date_of_birth.month < stud_rec[k-1].date_of_birth.month )
    swap(stud_rec, k, k-1 );
    else if(stud_rec[k].date_of_birth.month > stud_rec[k-1].date_of_birth.month );
    else if(stud_rec[k].date_of_birth.day < stud_rec[k-1].date_of_birth.day )
    swap(stud_rec,k, k-1);
    }
    }
    } /* sort_date */


    void print_record2(struct_stud stud_rec[], int j)
    {
    printf("%-15s : %s\n", "First Name", stud_rec[j].stud_firstname);
    printf("%-15s : %s\n", "Surname", stud_rec[j].stud_surname);
    printf("%-15s : %d\n", "ID number", stud_rec[j].stud_id);
    printf("%-15s : %d/%d/%d\n", "Date of birth", stud_rec[j].date_of_birth.day, stud_rec[j].date_of_birth.month, stud_rec[j].date_of_birth.year);
    }
    void print_statement(int tot_rec)
    {
    if (tot_rec!=0)
    printf("\n*** Total of %d record%c found ***\n", tot_rec, tot_rec==1?' ':'s');
    else printf("\n*** No record found ***\n");
    } /* print_statement */

    void swap(struct_stud *stud_rec, int a, int b)
    {
    struct_stud temp;
    temp = stud_rec[a];
    stud_rec[a] = stud_rec[b];
    stud_rec[b] = temp;
    } /* swap */

    int valid_date(const int dd, const int mm, const int yyyy)
    {
    int day_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int i, error = 0;

    for(i=1;i<=12;i++)
    {
    if (mm == i)
    {
    error = 0;
    break;
    }

    if (i==12 && mm != i)
    error = 1;
    }

    if (dd<1 || dd>day_of_month[mm-1])
    error = 1;

    if (mm == 2)
    {
    /* Check for leap year */
    if(!(yyyy%4)&&(yyyy%100)||!(yyyy%400))
    if(dd == 29)
    error = 0;

    }

    if(error == 1)
    printf("Invalid date!\n");

    return error;
    } /* valid_date */

    void convert_to_upper_array(char *ch)
    {
    while (*ch != '\0')
    {
    if (islower(*ch))
    *ch = toupper(*ch);

    ++ch;
    }
    } /* convert_to_upper_array */
    void convert_to_upper_char(char *ch)
    {
    if (islower(*ch))
    *ch = toupper(*ch);
    } /* convert_to_upper_char */

    void check_line_printed(int *line_printed, int max_line)
    {
    if (*line_printed >= max_line)
    {
    printf("Press any key to continue...");
    getch();
    printf("\n");
    *line_printed = 0;
    }
    } /* check_line_printed */

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > void main()

    You lost me there. And for the love of god, man, use some [code] tags!

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If your program is longer than 10 lines use the code tags. For future reference, 10 lines is this many:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    #include<ctype.h>
    #define SIZE 10
    struct stype {char s1[100], s2[100]; };
    struct stype svar[10];

    typedef struct{
    int day,month,year;

    And that's where I stopped reading. But judging from quzah's reaction, the first thing you can do to fix your code is to change void main() to int main ( void ) and you will magically keep your program from being completely undefined.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. literature database: help with planning
    By officedog in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2009, 12:34 PM
  2. Replies: 6
    Last Post: 07-07-2008, 07:48 AM
  3. TreeView and Database
    By x64 in forum C# Programming
    Replies: 2
    Last Post: 01-14-2007, 07:42 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. simple database app. in borland c++ builder tutorial
    By kariem2k in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2002, 07:28 AM