Struct ve Dosya İşlemleri

Konu, 'C Dersleri' kısmında mustaphos tarafından paylaşıldı.

  1. mustaphos

    mustaphos MB Üyesi

    Kayıt:
    14 Eylül 2015
    Mesajlar:
    34
    Beğeniler:
    7
    Meslek:
    Öğrenci
    En İyi Cevap:
    0
    Değerlendiriler:
    +9 / 1 / -0
    Üniversite:
    Anadolu University
    Merhabalar.
    Bu programımda struct ve dosya işlem operatörleri ile bir öğrenci veritabanı yaptım.
    Veritabanına erişip öğrenci kaydetme,silme,sıralama,arama,listeleme yapılabiliyor.
    İyi çalışmalar.

    /* Used libraries */
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>

    /* Function prototypes */
    void menu_function();
    void insert_function();
    void search_function();
    void delete_function();
    void list_function();

    /* Variables */
    int menu_value;
    char yes_or_no;
    int list_value;

    /* Struct */
    struct Students
    {
    char name[20];
    char surname[20];
    char grade[6];
    char id[12];
    };

    void main()
    {
    menu_function();/* Call the menu function */
    }

    void menu_function ()
    {
    yes_or_no='y';/* Define yes_or_no variable 'y' to get into the loop */

    while (yes_or_no=='y')
    {
    printf("Type [ 0 ] and press [ ENTER ] for help\n");
    printf("Type [ 1 ] and press [ ENTER ] for insert a record\n");
    printf("Type [ 2 ] and press [ ENTER ] for search a record\n");
    printf("Type [ 3 ] and press [ ENTER ] for delete a record\n");
    printf("Type [ 4 ] and press [ ENTER ] for list of records\n");
    printf("Type [ 5 ] and press [ ENTER ] to exit program\n");

    printf(">>> ");
    scanf("%d",&menu_value);/* Get the menu_value to call function cases */

    switch(menu_value)/* What user wants to do */
    {
    case 0:/* Insert a student */
    {
    printf("> The records saved in project2_database.bin \n");
    printf("> Search and delete functions are uppercase and lowercase sensetive. \n");
    printf("> If you want to find 'Mustafa' and search 'mustafa' the result not be found. \n");
    printf("> So in search and delete function, type exactly what you recorded. \n");
    printf("> The grade must be 0<grade<100. Not equal to 0 and 100. \n");
    printf("> If you add record 'mustafa gokce 99.99 11111111111' program works perfectly. \n");
    break;
    }

    case 1:/* Insert a student */
    {
    struct Students Student;

    printf("Please write students name:\n");
    printf(">>>");
    scanf("%s",&Student.name);
    printf("Please write students surname:\n");
    printf(">>>");
    scanf("%s",&Student.surname);
    printf("Please write students grade:\n");
    printf(">>>");
    scanf("%s",&Student.grade);
    printf("Please write students ID number:\n");
    printf(">>>");
    scanf("%s",&Student.id);

    insert_function(Student);/* Call insert function */

    break;
    }

    case 2:/* Find the student */
    {
    char find[20];

    printf("Please write the name,surname,grade or id number you want to find:\n");
    printf(">>>");
    scanf("%s",&find);/* Which value will be searched ? Sensible to lower and upper case, so must be searched as exactly as saved. */

    search_function(find);/* Call search function */

    break;
    }

    case 3:/* Erase a student */
    {
    char sil[20];

    printf("Please write the record's name,surname,grade or id number you want to delete:\n");
    printf(">>>");
    scanf("%s",&sil);/* Scan any data which student has and delete student which is owner of data */

    delete_function(sil);/* Call delete function */

    break;
    }

    case 4:/* List of records */
    {
    printf("Type [ 1 ] and press [ ENTER ] for names a-z\n");
    printf("Type [ 2 ] and press [ ENTER ] for surnames a-z\n");
    printf("Type [ 3 ] and press [ ENTER ] for grades 0-9\n");
    printf("Type [ 4 ] and press [ ENTER ] for id numbers 0-9\n");
    printf("Type [ 5 ] and press [ ENTER ] for list as recorded\n");

    printf(">>> ");
    scanf("%d",&list_value);/* Get the list_value to call function cases */

    list_function(list_value);/* Call list function */

    break;
    }


    case 5:
    {
    printf("Exiting the program\n");
    exit(0);/* Exit the program */
    }

    }
    yes_or_no='n';/* Define yes_or_no value n for ask continue or not */
    printf("Continue the program ? [ y ] for yes or [ n ] for no\n");
    printf(">>> ");
    scanf("%s",&yes_or_no);/* Get the yes_or_no value for continue the program */
    }
    }

    void insert_function(struct Students student)
    {
    FILE *print_file;
    print_file = fopen("project2_database.bin","ab");/* Open file for adding record */

    if(print_file==NULL)/* If file not exist than create */
    {
    printf("Error!");
    exit(1);
    }


    fprintf (print_file,"%s %s %s %s\n",student.name,student.surname,student.grade,student.id) ;/* Add records to file */

    fclose(print_file);/* Close file */

    printf("Students name:%s\n",student.name);
    printf("Students surname:%s\n",student.surname);
    printf("Students grade:%s\n",student.grade);
    printf("Students ID number:%s\n",student.id);
    }

    void search_function(char find[20])
    {
    struct Students student;

    FILE *read_file;
    read_file = fopen ( "project2_database.bin", "r" ) ;/* Open file for reading */

    if(read_file==NULL)/* If file not exist than create */
    {
    printf("Error!");
    exit(1);
    }

    else
    {
    printf("%-20s %-20s %-5s %-12s\n","NAME","SURNAME","GRADE","ID");
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;/* Scan file */

    while(!feof(read_file))
    {
    if(strcmp(find,student.name)==0||strcmp(find,student.surname)==0||strcmp(find,student.grade)==0||strcmp(find,student.id)==0)/* If there is any match */
    {
    printf("%-20s %-20s %-5s %-12s\n",&student.name,&student.surname,&student.grade,&student.id);/* Print the match */
    }
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;/* Scan until end of the file */
    }
    }

    fclose(read_file);/* Close file */

    }

    void delete_function(char sil[20])
    {
    struct Students student;
    struct Students delete_student[1000];/* For rewrite the file without deleted datas */
    int n=0;
    int sayac;

    FILE *read_file;
    FILE *print_file;
    read_file = fopen ( "project2_database.bin", "r" ) ;/* Open file for reading */

    if(read_file==NULL)/* If file not exist than create */
    {
    printf("Error!");
    exit(1);
    }

    else
    {
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;/* Scan datas in file */

    while(!feof(read_file))
    {
    delete_student[n]=student;/* Each rows recorded as a struct */
    n++;
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;/* Scan datas continuesly */
    }

    printf("%-20s %-20s %-5s %-12s\n","NAME","SURNAME","GRADE","ID");
    for(sayac=0;sayac<n;sayac++)
    {
    if(!(strcmp(sil,delete_student[sayac].name)==0||strcmp(sil,delete_student[sayac].surname)==0||strcmp(sil,delete_student[sayac].grade)==0||strcmp(sil,delete_student[sayac].id)==0))/* Print the datas which will not be deleted */
    {
    printf("%-20s %-20s %-5s %-12s\n",&delete_student[sayac].name,&delete_student[sayac].surname,&delete_student[sayac].grade,&delete_student[sayac].id);
    }
    }
    }
    fclose(fopen("project2_database.bin", "w"));/* Clean the file */
    fclose(read_file);

    print_file = fopen("project2_database.bin","ab");

    if(print_file==NULL)
    {
    printf("Error!");
    exit(1);
    }

    for(sayac=0;sayac<n;sayac++)
    {
    if(!(strcmp(sil,delete_student[sayac].name)==0||strcmp(sil,delete_student[sayac].surname)==0||strcmp(sil,delete_student[sayac].grade)==0||strcmp(sil,delete_student[sayac].id)==0))/* Write the datas which will not be deleted into the file */
    {
    fprintf (print_file,"%s %s %s %s\n",&delete_student[sayac].name,&delete_student[sayac].surname,&delete_student[sayac].grade,&delete_student[sayac].id) ;/* Write datas */
    }
    }

    fclose(print_file);
    }

    void list_function(int list_value)
    {
    struct Students student;
    static struct Students dynamic_student[1000];
    static struct Students dynamic_student2[1000];
    static struct Students dynamic_student3[1000];
    static struct Students dynamic_student4[1000];
    static struct Students student_temp;
    int n=0;
    int sayac;
    int sayac2;

    switch(list_value)
    {
    case 1:/* List names a-z */
    {

    FILE *read_file;
    read_file = fopen ( "project2_database.bin", "r" ) ;

    if(read_file==NULL)
    {
    printf("Error!");
    exit(1);
    }

    else
    {
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;/* Scan file */

    while(!feof(read_file))
    {
    dynamic_student[n]=student;/* Record the datas which are inside file to the dynamic struct */
    n++;
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;/* Scan file continiously */
    }

    for(sayac=0;sayac<n;sayac++)
    {
    for(sayac2=0;sayac2<n;sayac2++)
    {
    if(strcmp(dynamic_student[sayac2].name, dynamic_student[sayac2+1].name) > 0)/* If right name comes first in alphabetical order with left name than switch the datas */
    {
    student_temp=dynamic_student[sayac2+1];
    dynamic_student[sayac2+1]=dynamic_student[sayac2];
    dynamic_student[sayac2]=student_temp;

    }
    }
    }

    printf("%-20s %-20s %-5s %-12s\n","NAME","SURNAME","GRADE","ID");
    for(sayac=1;sayac<n+1;sayac++)
    {
    printf("%-20s %-20s %-5s %-12s\n",&dynamic_student[sayac].name,&dynamic_student[sayac].surname,&dynamic_student[sayac].grade,&dynamic_student[sayac].id);/* Print the alphabetical sorted datas */
    }
    }

    fclose(read_file);


    break;
    }

    case 2:
    {
    FILE *read_file;
    read_file = fopen ( "project2_database.bin", "r" ) ;

    if(read_file==NULL)
    {
    printf("Error!");
    exit(1);
    }

    else
    {
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;

    while(!feof(read_file))
    {
    dynamic_student2[n]=student;/* Record the datas which are inside file to the dynamic struct */
    n++;
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;
    }

    for(sayac=0;sayac<n;sayac++)
    {
    for(sayac2=0;sayac2<n;sayac2++)
    {
    if(strcmp(dynamic_student2[sayac2].surname, dynamic_student2[sayac2+1].surname) > 0)/* Compare surnames */
    {
    student_temp=dynamic_student2[sayac2+1];
    dynamic_student2[sayac2+1]=dynamic_student2[sayac2];
    dynamic_student2[sayac2]=student_temp;

    }
    }
    }

    printf("%-20s %-20s %-5s %-12s\n","NAME","SURNAME","GRADE","ID");
    for(sayac=1;sayac<n+1;sayac++)
    {
    printf("%-20s %-20s %-5s %-12s\n",&dynamic_student2[sayac].name,&dynamic_student2[sayac].surname,&dynamic_student2[sayac].grade,&dynamic_student2[sayac].id);/* Print datas in surnames alphabetical order */
    }
    }

    fclose(read_file);

    break;
    }

    case 3:
    {
    FILE *read_file;
    read_file = fopen ( "project2_database.bin", "r" ) ;

    if(read_file==NULL)
    {
    printf("Error!");
    exit(1);
    }

    else
    {
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;

    while(!feof(read_file))
    {
    dynamic_student3[n]=student;/* Record the datas which are inside file to the dynamic struct */
    n++;
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;
    }

    for(sayac=0;sayac<n;sayac++)
    {
    for(sayac2=0;sayac2<n;sayac2++)
    {
    if(strcmp(dynamic_student3[sayac2].grade, dynamic_student3[sayac2+1].grade) > 0)/* Compare grades */
    {
    student_temp=dynamic_student3[sayac2+1];
    dynamic_student3[sayac2+1]=dynamic_student3[sayac2];
    dynamic_student3[sayac2]=student_temp;

    }
    }
    }

    printf("%-20s %-20s %-5s %-12s\n","NAME","SURNAME","GRADE","ID");
    for(sayac=1;sayac<n+1;sayac++)
    {
    printf("%-20s %-20s %-5s %-12s\n",&dynamic_student3[sayac].name,&dynamic_student3[sayac].surname,&dynamic_student3[sayac].grade,&dynamic_student3[sayac].id);/* Print records in ascending grades */
    }
    }

    fclose(read_file);

    break;

    }

    case 4:
    {
    FILE *read_file;
    read_file = fopen ( "project2_database.bin", "r" ) ;

    if(read_file==NULL)
    {
    printf("Error!");
    exit(1);
    }

    else
    {
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;

    while(!feof(read_file))
    {
    dynamic_student4[n]=student;/* Record the datas which are inside file to the dynamic struct */
    n++;
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;
    }

    for(sayac=0;sayac<n;sayac++)
    {
    for(sayac2=0;sayac2<n;sayac2++)
    {
    if(strcmp(dynamic_student4[sayac2].id, dynamic_student4[sayac2+1].id) > 0)/* Compare ID numbers */
    {
    student_temp=dynamic_student4[sayac2+1];
    dynamic_student4[sayac2+1]=dynamic_student4[sayac2];
    dynamic_student4[sayac2]=student_temp;

    }
    }
    }

    printf("%-20s %-20s %-5s %-12s\n","NAME","SURNAME","GRADE","ID");
    for(sayac=1;sayac<n+1;sayac++)
    {
    printf("%-20s %-20s %-5s %-12s\n",&dynamic_student4[sayac].name,&dynamic_student4[sayac].surname,&dynamic_student4[sayac].grade,&dynamic_student4[sayac].id);/* Print datas in ascending ID form */
    }
    }

    fclose(read_file);

    break;
    }

    case 5:
    {
    FILE *read_file;
    read_file = fopen ( "project2_database.bin", "r" ) ;

    if(read_file==NULL)
    {
    printf("Error!");
    exit(1);
    }

    else
    {
    printf("%-20s %-20s %-5s %-12s\n","NAME","SURNAME","GRADE","ID");
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;

    while(!feof(read_file))
    {
    printf("%-20s %-20s %-5s %-12s\n",&student.name,&student.surname,&student.grade,&student.id);/* Print datas as recorded */
    fscanf (read_file, "%s %s %s %s",&student.name,&student.surname,&student.grade,&student.id) ;
    }
    }

    fclose(read_file);

    break;
    }
    }
    }