Thread: Student record system; file handling

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Cagayan de Oro, Philippines, Philippines
    Posts
    1

    Exclamation Student record system; file handling

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    
    struct student{
        int ID_no;
        char fname[20];
        char mname[20];
        char lname[20];
        char course_year[20];
        char bdate[20];
        char contnum[20];  
        char mark[20];
    };
    
    
    struct student stu;
    
    
    
    
     COORD coord = {0,0}; ///set the cordinate to 0, 0 (top-left corner of window);
    void gotoxy(int x, int y){
        coord.X = x; coord.Y = y; /// X and Y coordinates
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    
     void SetColor(int ForgC)
    {
         WORD wColor;
         ///We will need this handle to get the current background attribute
         HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
         CONSOLE_SCREEN_BUFFER_INFO csbi;
    
    
         ///We use csbi for the wAttributes word.
         if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
         {
            ///Mask out all but the background attribute, and add in the forgournd color
              wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
              SetConsoleTextAttribute(hStdOut, wColor);
         }
         return;
    }
    
    
    void header(){
        
        system("cls");
        SetColor(23);
        gotoxy(28,2);
        printf("STUDENT RECORD SYSTEM");
        gotoxy(25,3);
        printf("COLLEGE OF COMPUTER STUDIES");
        gotoxy(21,4);
        printf("XAVIER UNIVERSITY - ATENEO DE CAGAYAN");
        SetColor(14);
    }
    
    
    int main(){
    
    
            int flag,choice,found,continu,length,select,isFound=0;
            float gra;                                             /* Main menu */
            char student_lname[20];
            int IDno;
            int align_ask=5,align_menu=20;
            FILE *fp;
            FILE *origF;
            FILE *newF;
                    do{    
                        
                        system("cls");
                        header();
                        
                        gotoxy(align_menu,7);printf("\n\t\t    ======================================");
                        gotoxy(align_menu,8);printf("\n\t\t    |              CHOICES:              |");
                        gotoxy(align_menu,9);printf("\n\t\t    |                          |");
                        gotoxy(align_menu,10);printf("\n\t\t    |  [1] Add Student                   |");
                        gotoxy(align_menu,11);printf("\n\t\t    |  [2] Search Student Record         |");
                        gotoxy(align_menu,12);printf("\n\t\t    |  [3] Modify Student Record         |");
                        gotoxy(align_menu,13);printf("\n\t\t    |  [4] Update Student Mark           |");
                        gotoxy(align_menu,14);printf("\n\t\t    |  [5] Delete Student                |");
                        gotoxy(align_menu,15);printf("\n\t\t    |  [6] Letter Grade Converter       |");
                        gotoxy(align_menu,16);printf("\n\t\t    |  [7] Change Database Password      |");
                        gotoxy(align_menu,17);printf("\n\t\t    |  [8] Exit Program                  |");
                        gotoxy(align_menu,18);printf("\n\t\t    ======================================");
                        SetColor(10);printf("\n\t\t\tChoice: ");
                        scanf("%d", &select);
                            
                            if(select==1){                                                                                    /* Choice1 */
                                
                                while(choice!=2){
                                    
                                header();
                                
                                gotoxy(align_menu,7);printf("\n\t\t    ======================================");
                                gotoxy(align_menu,8);printf("\n\t\t    |                MENU:               |");
                                gotoxy(align_menu,9);printf("\n\t\t    |                          |");
                                gotoxy(align_menu,10);printf("\n\t\t    |  [1] Add New Student               |");
                                gotoxy(align_menu,11);printf("\n\t\t    |  [2] Back to Main Menu            |");
                                gotoxy(align_menu,12);printf("\n\t\t    ======================================");
                                SetColor(10);printf("\n\t\t\tChoice: ");
                                    scanf("%d", &choice);
                                    
                                    switch(choice){
                                        
                                        case 1:
                                            system("cls");
                                            header();
                                            gotoxy(align_menu,5);printf("\n\t\t\t\t-ADD STUDENT-");
                                            gotoxy(align_menu,6);printf("\n\t\tID no: ");
                                            scanf("%d", &stu.ID_no);
                                            gets(stu.fname);
                                            gotoxy(align_menu,7);printf("\n\t\tFirst Name: ");
                                            gets(stu.fname);
                                            gotoxy(align_menu,8);printf("\n\t\tLast Name: ");
                                            gets(stu.lname);
                                            gotoxy(align_menu,9);printf("\n\t\tCourse and Year: ");
                                            gets(stu.course_year);
                                            gotoxy(align_menu,10);printf("\n\t\tBirthdate(MM-DD-YY): ");
                                            gets(stu.bdate);
                                            gotoxy(align_menu,11);printf("\n\t\tContact Number: ");
                                            gets(stu.contnum);
                                            gotoxy(align_menu,12);printf("\n\t\tCurrent Mark(1-100): ");
                                            gets(stu.mark);
                                            
                                            fp=fopen("studentfile.txt","a+");    
                                            fprintf(fp,"\nID no:          %d\n",stu.ID_no); 
                                            fprintf(fp,"First Name:     %s\n",stu.fname);  
                                            fprintf(fp,"Last Name:          %s\n",stu.lname); 
                                            fprintf(fp,"Course and Year:  %s\n",stu.course_year); 
                                            fprintf(fp,"Birthdate:          %s\n",stu.bdate); 
                                            fprintf(fp,"Contact Number:      %s\n\n",stu.contnum); 
                                            fprintf(fp,"Current Mark:      %s\n\n",stu.mark); 
                                            fclose(fp);
                                            
                                            system("cls");
                                            
                                            MessageBox(0,"A new student record has been added successfully","Student Record System",0);
                                    }
                                }
                            }
                            
                        
                            choice=1;
                        
                            if(select==2){                                                                                                /* Choice2 */
                                
                                while(choice!=2){
                                    header();
                                    
                                    gotoxy(align_menu,7);printf("\n\t\t    ======================================");
                                    gotoxy(align_menu,8);printf("\n\t\t    |                MENU:               |");
                                    gotoxy(align_menu,9);printf("\n\t\t    |                          |");
                                    gotoxy(align_menu,10);printf("\n\t\t    |  [1] Search A Student              |");
                                    gotoxy(align_menu,11);printf("\n\t\t    |  [2] Back to Main Menu            |");
                                    gotoxy(align_menu,12);printf("\n\t\t    ======================================");
                                    SetColor(10);printf("\n\t\t\tChoice: ");
                                    scanf("%d", &choice);
                                    
                                    switch(choice){
                                        case 1:{
                                               system("cls");
                                               header();
                                               gotoxy(align_menu,5);printf("\n\t\t\t\t-SEARCH STUDENT-");
                                               printf("\nEnter the Student's ID no. :");
                                               scanf("%d",&IDno);
                                               MessageBox(0,"Searching records...","Student Record System",0);
                                                   found=0;
                                                if((fp=fopen("studentfile.txt","r"))==NULL){
                                                    printf(" ! The File is Empty...\n\n");
                                                }
                                                else{
                                                    while(!feof(fp)&& found==0){
                                                        fscanf(fp,"\nID no:          %d\n",&stu.ID_no); 
                                                        fscanf(fp,"First Name:     %s\n",stu.fname);  
                                                        fscanf(fp,"Last Name:          %s\n",stu.lname); 
                                                        fscanf(fp,"Course and Year:  %s\n",stu.course_year); 
                                                        fscanf(fp,"Birthdate:          %s\n",stu.bdate); 
                                                        fscanf(fp,"Contact Number:      %s\n\n",stu.contnum); 
                                                        fscanf(fp,"Current Mark:      %s\n\n",stu.mark); 
                                                        if(stu.ID_no==IDno)
                                                            found=1;
                                                    }
                                                }
                                               if(found){
                                                       system("cls");
                                                       header();
                                                    MessageBox(0,"A record was found!","Student Record System",0);
                                                    gotoxy(align_menu,10);printf("ID no:          %d\n",stu.ID_no); 
                                                    gotoxy(align_menu,11);printf("First Name:     %s\n",stu.fname);  
                                                    gotoxy(align_menu,12);printf("Last Name:          %s\n",stu.lname); 
                                                    gotoxy(align_menu,13);printf("Course and Year:  %s\n",stu.course_year); 
                                                    gotoxy(align_menu,14);printf("Birthdate:          %s\n",stu.bdate); 
                                                    gotoxy(align_menu,15);printf("Contact Number:      %s\n\n",stu.contnum); 
                                                    gotoxy(align_menu,16);printf("Current Mark:      %s\n\n",stu.mark); 
                                                    getch();
                                               }
                                               else{
                                                 MessageBox(0,"ID no does not exist!","Student Record System",0);
                                                 getch();
                                               }
                                               fclose(fp);
                                            break;
                                        }
                                    }
                                }
                            }
                            choice=1;
                            if(select==3){                                                                                                /* Choice3 */
                                
                                while(choice!=2){
                                    system("cls");
                                    header();
                                    gotoxy(align_menu,7);printf("\n\t\t    ======================================");
                                    gotoxy(align_menu,8);printf("\n\t\t    |                MENU:               |");
                                    gotoxy(align_menu,9);printf("\n\t\t    |                          |");
                                    gotoxy(align_menu,10);printf("\n\t\t    |  [1] Modify A Student              |");
                                    gotoxy(align_menu,11);printf("\n\t\t    |  [2] Back to Main Menu            |");
                                    gotoxy(align_menu,12);printf("\n\t\t    ======================================");
                                    SetColor(10);printf("\n\t\t\tChoice: ");
                                    scanf("%d", &choice);
        
                                    
                                    switch(choice){
                                        case 1:{
                                            system("cls");
                                            header();
                                            gotoxy(align_menu,5);printf("\n\t\t\t\t-MODIFY STUDENT-");
                                            printf("\nEnter the Student's ID no. :");
                                            scanf("%d",&IDno);
                                            
                                             if((fp=fopen("studentfile.txt","rb+"))==NULL){
                                                    printf(" ! The File is Empty...\n\n");
                                            }
                                            else{
                                                while(fread(&stu,sizeof(stu),1,fp) == 1){
                                                    if(IDno==stu.ID_no){
                                                        gotoxy(align_menu,6);printf("\n\t\tID no: ");
                                                        scanf("%d", &stu.ID_no);
                                                        gets(stu.fname);
                                                        gotoxy(align_menu,7);printf("\n\t\tFirst Name: ");
                                                        gets(stu.fname);
                                                        gotoxy(align_menu,8);printf("\n\t\tLast Name: ");
                                                        gets(stu.lname);
                                                        gotoxy(align_menu,9);printf("\n\t\tCourse and Year: ");
                                                        gets(stu.course_year);
                                                        gotoxy(align_menu,10);printf("\n\t\tBirthdate(MM-DD-YY): ");
                                                        gets(stu.bdate);
                                                        gotoxy(align_menu,11);printf("\n\t\tContact Number: ");
                                                        gets(stu.contnum);
                                                        gotoxy(align_menu,12);printf("\n\t\tCurrent Mark(1-100): ");
                                                        gets(stu.mark);
                                                        fseek(fp,-sizeof(stu), SEEK_CUR);
                                                        fwrite(&stu,sizeof(stu), 1, fp);
                                                        isFound = 1;
                                                        fclose(fp);
                                                    }
                                                }
                                            }
                                            if(!isFound){
                                                 MessageBox(0,"No record found!","Student Record System",0);
                                            }
                                            fclose(fp);
                                            break;
                                        }
                                    }
                                }
                            }
                        
                        choice=1;
                        
                        if(select==4){                                                                                                    /* Choice4 */    
                            MessageBox(0,"UNDER CONSTRUCTION!","Student Record System",0);
                        }
                        
                        choice=1;
                        
                        if(select==5){                                                                                                    /* Choice5 */
                                
                                while(choice!=2){
                                    system("cls");
                                    header();
                                    gotoxy(align_menu,7);printf("\n\t\t    ======================================");
                                    gotoxy(align_menu,8);printf("\n\t\t    |                MENU:               |");
                                    gotoxy(align_menu,9);printf("\n\t\t    |                          |");
                                    gotoxy(align_menu,10);printf("\n\t\t    |  [1] Delete A Student              |");
                                    gotoxy(align_menu,11);printf("\n\t\t    |  [2] Back to Main Menu            |");
                                    gotoxy(align_menu,12);printf("\n\t\t    ======================================");
                                    
                                    printf("\n     Choice: ");
                                    scanf("%d", &choice);
                                    
                                    switch(choice){
                                        case 1:{
                                            system("cls");
                                            header();
                                            gotoxy(align_menu,5);printf("\n\t\t\t\t-DELETE STUDENT-");
                                            origF=fopen("studentfile.txt","rb");
                                            newF = fopen("studentfile_temp.txt","wb");
                                            printf("\nEnter the Student's ID no. :");
                                            scanf("%d",&IDno);
                                                while(fread(&stu, sizeof(stu),1,fp) == 1){
                                                    if((IDno, stu.ID_no) == 0){
                                                        fwrite(&stu,sizeof(stu),1,newF);
                                                    }
                                                }
                                        }
                                            fclose(origF);
                                            fclose(newF);
                                            remove("studentfile.txt");
                                            rename("studentfile_temp.txt","studentfile.txt");
                                            MessageBox(0,"Student Record Sucessfully Deleted!","Student Record System",0);
                                            break;    
                                    }
                                }
                        }
                    
                        
                        choice=1;
                        
                        if(select==6){                                                                                                    /* Choice6*/
                                
                                while(choice!=2){
                                    system("cls");
                                    header();
                                    
                                    gotoxy(align_menu,7);printf("\n\t\t   ============================================");
                                    gotoxy(align_menu,8);printf("\n\t\t   |                 MENU:                    |");
                                    gotoxy(align_menu,9);printf("\n\t\t   |                                |");
                                    gotoxy(align_menu,10);printf("\n\t\t   | [1] Convert Number Grade to Letter Grade |");
                                    gotoxy(align_menu,11);printf("\n\t\t   | [2] Back to Main Menu                 |");
                                    gotoxy(align_menu,12);printf("\n\t\t   ============================================");
                                    
                                    printf("\n\t\t\t  Choice: ");
                                    scanf("%d", &choice);
                                    
                                    switch(choice){
                                        case 1:{
                                            system("cls");
                                            header();
                                            printf("\n\n\n\n\t\tEnter Number Grade: ");
                                            scanf("%f", &gra);
                                        
                                            if(gra<=49.99){
                                                printf("\n\n\t\t\tThe Letter Grade Equivalence of %.2f is F.\n", gra);
                                                MessageBox(0,"FAILED!","Student Record System",0);
                                            }
                                            else if(gra<=58.99){
                                                printf("\n\n\t\t\tThe Letter Grade Equivalence of %.2f is D.\n", gra);
                                                MessageBox(0,"PASSED!","Student Record System",0);
                                            }
                                            else if(gra<=67.99){
                                                printf("\n\n\t\t\tThe Letter Grade Equivalence of %.2f is C.\n", gra);
                                                MessageBox(0,"SATISFACTORY!","Student Record System",0);
                                            }
                                            else if(gra<=75.99){
                                                printf("\n\n\t\t\tThe Letter Grade Equivalence of %.2f is B-.\n", gra);
                                                MessageBox(0,"FAIR!","Student Record System",0);;
                                            }
                                            else if(gra<=83.99){
                                                printf("\n\n\t\t\tThe Letter Grade Equivalence of %.2f is B.\n", gra);
                                                MessageBox(0,"GOOD!","Student Record System",0);
                                            }    
                                            else if(gra<=91.99){
                                                printf("\n\n\t\t\tThe Letter Grade Equivalence of %.2f is A-.\n", gra);
                                                MessageBox(0,"VERY GOOD!","Student Record System",0);
                                            }
                                            else if(gra<=100){
                                                printf("\n\n\t\t\tThe Letter Grade Equivalence of %.2f is A.\n", gra);
                                                MessageBox(0,"EXCELLENT!","Student Record System",0);
                                            }
                                            else{
                                                system("cls");
                                                MessageBox(0,"OUT OF RANGE!","Student Record System",0);
                                                
                                            }
                                            getch();            
                                        }
                                    }
                                }
                        }
                        
                        choice=1;
                        
                        if(select==7){                                                                                                    /* Choice7 */
                            MessageBox(0,"UNDER CONSTRUCTION!","Student Record System",0);
                        }
                        
                        choice=1;
                    }while(select!=8);
            
    
    
    return 0;
    }
    Hey guys, i would like to ask for a help.

    I dont know how to modify the previous record that i have done in the Add Student function. i searched from different sites and got this code for modifying a record in a text file. But, i cant figure it out why its not working. could you help me please?


    I REALLY NEED YOUR HELP.
    Last edited by dirtyhabit_12; 02-25-2013 at 01:46 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You see those nice little functions like gotoxy() and SetColor() ?

    A development process

    You need some more, with names like
    addStudent()
    searchStudent()
    modifyStudent()
    and so on.

    Then your main doesn't become an impenetrable wall of code 100's of lines long.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by dirtyhabit_12 View Post
    But, i cant figure it out why its not working. could you help me please?
    Maybe, if you tell us what's not working.

    A quick scan shows several problems:
    1. Non-standard header files (conio.h, Windows.h)
    2. FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    3. FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    4. No functions / code repetition
    5. Inconsistent indentation

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Student record ; file handling
    By dnates2012 in forum C Programming
    Replies: 4
    Last Post: 02-23-2013, 12:13 AM
  2. Student GPA system problems
    By ShiroiShu in forum C Programming
    Replies: 10
    Last Post: 11-28-2011, 10:55 AM
  3. Student record program
    By mugiwara528 in forum C Programming
    Replies: 1
    Last Post: 03-14-2011, 06:27 PM
  4. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 07:22 AM
  5. I need help on Student record prog
    By Hursh in forum C Programming
    Replies: 2
    Last Post: 01-18-2002, 07:37 AM

Tags for this Thread