Thread: Student record ; file handling

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Student record ; file handling

    Code:
     #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    
     
    struct{
        int roll_no;
        char firstname[20];
        char lastname[20];
        int mark;
    }student;
     
    
    
     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 get_password(char* pass)
    {
        char temp_passP[25];
        int i=0;
         while(1)
        {
                temp_passP[i]=getch();
                if(temp_passP[i]==13){break;}
                else if(temp_passP[i]==8)
                {
                    if(i!=0) {
                    printf("\b \b");
                    i--;
                    } else {printf("\a");}
                }
                else
                {
                    printf("*");
                    *(pass+i) = temp_passP[i];
                    i++;
                }
                 *(pass+i)='\0';
         }
    }
    
    
    
    
    void program(){
    
    
    int flag,choice,shift,rollnumber,found,continu,length;                                             /* Main menu */
    char studentname[20];
    FILE *fp;
    printf("-------------------------------------------------------------\n");
    printf("----------------made by C code champ ------------------------\n");
    printf("-------------------------------------------------------------\n");
    printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
    Label1:
    printf("\n1 -> Store a new record in database\n");
    printf("2 -> Search a student record by Student First Name\n");
    printf("3 -> Search a student record by rollNo\n");
    printf("4 -> Quit Student Database");
    printf("\n\n");
    printf("Enter your choice : ");
    scanf("%d",&choice);
    switch(choice)
    {
    case  1:                                                                                        /*choice 1*/
    Label2:
    printf("\nEnter Student Details:\n\nRoll number (Integer): ");
    scanf("%d",&student.roll_no);
    printf("\nName:");
    scanf("%s",student.firstname);
    printf("\nSurname:");
    scanf("%s",student.lastname);
    printf("\nMark(0 - 100 integer) : ");
    scanf("%d",&student.mark);
    fp=fopen("studentfile.txt","a+");
    fprintf(fp,"\n%d\t%s\t%s\t%d\t",student.roll_no,student.firstname,student.lastname,student.mark);
    fclose(fp);
    printf("A student record has been added successfully...\n");
    printf("\n\n1 -> Wish to add another record to database");
    printf("\n2 -> Wish to move to Main Menu");
    printf("\n3 -> Exit from Program\n");
    scanf("%d",&shift);
    if(shift==1)
    goto Label2;
    if(shift==2)
    goto Label1;
    if(shift==3)
    break;
    if(shift!=1&&2&&3){
    printf("Exiting.........");
    break;
    }
     
    case 2:                                                                            /*choice 2*/
    Label4:
    printf("\nEnter student first name: ");
    scanf("%s",&studentname);
    printf("Searching record with studentname=%s.\n",studentname);
    found=0;
    if((fp=fopen("studentfile.txt","r"))==NULL)
    {
    printf(" ! The File is Empty...\n\n");
    }
    else
    {
    while(!feof(fp)&& found==0)
    {
    fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.roll_no,student.firstname,student.lastname,&student.mark);
    length = strlen(student.firstname);
    if(student.firstname[length]==studentname[length])
    found=1;
    }
    }
    if(found)
    {
    printf("\nThe record is found.");
    printf("\nRoll no: %d\nName: %s\nSurname: %s\nMark: %d \n",student.roll_no,student.firstname,student.lastname,student.mark);
    }
    else
    {
    printf("Not found...\n");
    getch();
    }
    Label5:
    printf("\n\n1 -> Wish to search another record");
    printf("\n2 -> Wish to move to Main Menu");
    printf("\n3 -> Exit from Program\n");
    scanf("%d",&shift);
    if(shift==1)
    goto Label4;
    if(shift==2)
    goto Label1;
    if(shift==3)
    break;
    if(shift!=1&&2&&3){
    printf("\nEnter a valid choice");
    goto Label5;
    }
    case 3:                                                                            /*choice 3*/
    Label6:
    printf("\nEnter the rollnumber: ");
    scanf("%d",&rollnumber);
    printf("Searching record with rollnumber=%d.\n",rollnumber);
    found=0;
    if((fp=fopen("studentfile.txt","r"))==NULL)
    {
    printf(" ! The File is Empty...\n\n");
    }
    else
    {
    while(!feof(fp)&& found==0)
    {
    fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.roll_no,student.firstname,student.lastname,&student.mark);
    if(student.roll_no==rollnumber)
    found=1;
    }
    }
    if(found)
    {
    printf("\nThe record is found.");
    printf("\nRoll no: %d\nName: %s\nSurname: %s\nMark: %d \n",student.roll_no,student.firstname,student.lastname,student.mark);
    }
    else
    {
    printf("Not found...\n");
    getch();
    }
    Label7:
    printf("\n\n1 -> Wish to search more..");
    printf("\n2 -> Wish to move to Main Menu");
    printf("\n3 -> Exit from Program\n");
    scanf("%d",&shift);
    if(shift==1)
    goto Label6;
    if(shift==2)
    goto Label1;
    if(shift==3)
    break;
    if(shift!=1&&2&&3){
    printf("\nEnter a valid choice");
    goto Label7;
    }
    case 4: break;                                                                    /*choice 4*/
    default :
    printf(" Bad choice...Enter the choice again...\n");
    goto Label1;
    }
    return 0;
    }
    
    
    
    
    int main()
    {
    char pass2[20],pass[20];
    int x = 15, y = 16;
    int grant=0,length;
    FILE *f;
        
    SetConsoleTitle("Student Record System");
    SetColor(10);
    gotoxy(13,9);printf(" For your own use, Please enter your personal password");
    SetColor(14);
    gotoxy(20,x);printf("PERSONAL PASSWORD:");    
    f=fopen("pass.txt","w");    
    gotoxy(39,x);
    SetColor(12);get_password(pass2);
    fprintf(f,"%s",pass2);
    fclose(f);
    
    
    system("cls");
        
    enter_pass:
    SetColor(10);
    gotoxy(21,8);printf(" The database is password protected.");
    gotoxy(21,9);printf(" Please enter you personal password");
    SetColor(15);
    gotoxy(24,x);printf("PASSWORD:");
    gotoxy(34,x);SetColor(12);get_password(pass);
        
    f=fopen("pass.txt","r");
    
    
    while(!feof(f)&& grant==0){
    fscanf(f,"%s",pass);
    length = strlen(pass2);
    if(pass2[length]==pass[length])
    grant=1;
    }
    
    
    if(grant){
    system("cls");
    program();
    }
    else{
    system("cls");
    MessageBox(0,"YOU HAVE ENTERED THE WRONG PASSWORD! Click Ok, to enter again","Student Record System",0);
    goto enter_pass;
    }
    
    
    fclose(f);
    getch();
    return 0;
    }
    PROBLEMS :

    1.) This is our source code, the problem here is that when we are prompt to enter "personal password" we successfully got in, but it does not identify any wrong passwords.

    ex. "Please enter a password"
    ---- password
    "type your password:"
    ----password1 (Even if i typed the wrong password I registered earlier it still lets me into the program menu.)

    I will post further problems if I find more.. ;D please do help me thank you.,.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Where do you compare the two passwords?
    I saw no strcmp in your code.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    Where do you compare the two passwords?
    I saw no strcmp in your code.

    Tim S.
    i added this set of codes, but it still doesn't work.

    Code:
    while(!feof(f)&& grant==0){
    		fscanf(f,"%s",pass);
    		if(strcmp(pass,pass2)==0){
    			grant=1;
    			}
    	}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh please, you've just mashed together bits of code from several places and now you're asking us to fix it for you.
    C program of Student Database System

    I don't know whether to laugh or cry - when someone calls themselves "c code champ" and writes such a train-wreck of gotos
    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.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Ummm....
    You prompt for a password, store it in pass2 and write it out to a file:

    Code:
    f=fopen("pass.txt","w"); 
    get_password(pass2);
    fprintf(f,"%s",pass2);
    Then you prompt for a password and store it in pass.
    Code:
    get_password(pass);
    But then you reaopen the file and read the saved password into pass. So you're just comparing what you read from the file to what you wrote to the file earlier.

    Code:
    f=fopen("pass.txt","r");
    fscanf(f,"%s",pass);
    strcmp(pass, pass2);
    strcmp the right two strings and this bit will probably work, wouldn't like to speculate on the rest of the program though...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. Student record program
    By mugiwara528 in forum C Programming
    Replies: 1
    Last Post: 03-14-2011, 06:27 PM
  3. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 07:22 AM
  4. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 06:42 PM
  5. I need help on Student record prog
    By Hursh in forum C Programming
    Replies: 2
    Last Post: 01-18-2002, 07:37 AM