0% found this document useful (0 votes)
23 views5 pages

Main 1

This C program allows a user to manage student records by adding, searching, and marking student data. The main functions include adding a new student record by prompting the user to input student ID, name, address, phone, and email. Student records are stored in text files. Other functions allow searching for a student by ID, checking if an ID already exists, displaying the menu options, and adding marks for a student in a particular subject. The program uses structures to store student data and file handling functions to read and write to text files for data persistence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Main 1

This C program allows a user to manage student records by adding, searching, and marking student data. The main functions include adding a new student record by prompting the user to input student ID, name, address, phone, and email. Student records are stored in text files. Other functions allow searching for a student by ID, checking if an ID already exists, displaying the menu options, and adding marks for a student in a particular subject. The program uses structures to store student data and file handling functions to read and write to text files for data persistence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>

#define SON 100


#define SOE 100

COORD coord={0,0};

void gotoxy(int x,int y)


{
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

char temp[120]="";
char temp2[20]="";
typedef struct
{
char name[SON],email[SOE],ph[10],adr[10],id[9];
} STU;

int add_student ();


int searchstu(char * id);
int idsearch ();
int idcheck (char * idadd);
int print();
int choices ();
int addmarks ();
int subjects ();

int main()
{
while (1)
choices();
getc(stdin);
return 0;
}

int add_student ()
{
STU stu;
FILE *add,*idstu;
int check;
char save,cont;

int len;
while(1)
{
save='n';
system("cls");
printf("enter the ID number of student :");
fflush(stdin);
gets(stu.id);
check=idcheck (stu.id);
if (check)
{
printf("the student is exit :\n");
searchstu(stu.id );
}

else
{
printf("enter the name of student :");
gets(stu.name);
len=strlen(stu.name);
stu.name[len]='\0';
printf("enter the address of student :");
gets(stu.adr);
printf("enter the phone of student :");
gets(stu.ph);
printf("enter the email of student :");
gets(stu.email);
len=strlen(stu.email);
stu.email[len]='\0';

printf ("\npress 's' to save :");


scanf(" %c",&save);
}

if (save=='s')
{
idstu=fopen("id_number.txt","r+");
if (idstu == NULL)
idstu=fopen("id_number.txt","w");

add=fopen("data_of_student.txt","r+");
if (add == NULL)
add=fopen("data_of_student.txt","w");

fseek(idstu,-3,SEEK_END);
fseek(add,-3,SEEK_END);

fprintf(idstu,"%s\t",stu.id);
fprintf(add,"%s\t%s.\t%s\t%s\t%s\n\n",stu.id,stu.name,stu.adr,stu.ph,stu.
email);

fprintf(idstu ,"%s","eof");
fprintf(add ,"%s","eof");

fclose(idstu);
fclose(add);

printf ("\npress 'Y' to add another student :");


scanf(" %c",&cont);
if (cont!='y' && cont!='Y') break;

}
// fprintf(idstu ,"%s","eof");
// fprintf(add ,"%s","eof");
//
// fclose(idstu);
// fclose(add);
return 0 ;
}

int searchstu(char * id){


FILE *searchstud;
STU student;
searchstud=fopen("data_of_student.txt","r");
fscanf(searchstud,"%s",temp);
while(strcmp(temp,id))
{
fscanf(searchstud,"%s",temp);
if (!strcmp(temp,"eof"))
{
fclose(searchstud);
return 0;
}
}
fscanf(searchstud,"%[^.]s",student.name);
fscanf(searchstud,".%s",student.adr);
fscanf(searchstud,"%s",student.ph);
fscanf(searchstud,"%s",student.email);
printf("\nStudent Name: %s\nStudent Address: %s\nStudent Phone: %s\nStudent Email:
%s",student.name,student.adr,student.ph,student.email);

fclose(searchstud);
getc(stdin);
getc(stdin);

return 1;
}

int idsearch ()
{
system ("cls");
printf("enter the id number to search :");
scanf("%s",temp2);
searchstu(temp2);

int idcheck (char * idadd)


{
char idch[10];
FILE *id;
id=fopen("id_number.txt","r+");
if (id == NULL) return 0;
while (1)
{
fscanf(id,"%s",idch);
if (!strcmp(idch,"eof")) return 0;
if (!strcmp(idch,idadd))return 1;
}
}

int print()
{
system("cls");
int choice;
gotoxy(29,7);
printf("[1] add student .");
gotoxy(29,8);
printf("[2] search of student .");
gotoxy(29,9);
printf("[3] add marks .");
gotoxy(29,10);
printf("[4] Exit .");
gotoxy(29,20);
printf("enter number between 1 and 4 :");
gotoxy(29,21);
printf("enter your choice : ");
scanf("%d",&choice);
return choice;
}

int choices ()
{
int choice;
choice=print();
if (choice == 1) add_student();
else if (choice == 2)idsearch ();
else if (choice == 3)addmarks();
else if (choice == 4) exit(1);
else print();

int subjects ()
{
int sub;
system("cls");
gotoxy(29,7);
printf("[1] Mathematics :");
gotoxy(29,8);
printf("[2] C language :");
gotoxy(29,9);
printf("[3] Fluid mechanics :");
gotoxy(29,10);
printf("[4] Electronics :");
gotoxy(29,11);
printf("[5] Electric circuit :");
gotoxy(29,14);
printf ("enter number between 1 and 5");
gotoxy(29,15);
printf("choose the subject : ");
scanf("%d",&sub);
if(sub>5)
subjects();
if (sub<0)
subjects();
return sub;
}

int addmarks ()
{
int sub;
sub=subjects();
char check[10];
system("cls");
FILE *markf,*idm;
char idmark[10];
float mark;
if(sub==1)
{
markf=fopen("mathematics.txt","r+");
if (markf == NULL)
markf=fopen("mathematics.txt","w");
}
else if(sub==2)
{
markf=fopen("c_language.txt","r+");
if (markf == NULL)
markf=fopen("c_language.txt","w");
}
else if(sub==3)
{
markf=fopen("fluid mechanics.txt","r+");
if (markf == NULL)
markf=fopen("fluid mechanics.txt","w");
}
else if(sub==4)
{
markf=fopen("electronics.txt","r+");
if (markf == NULL)
markf=fopen("electronics.txt","w");
}
else if(sub==5)
{
markf=fopen("electric_circuit.txt","r+");
if (markf == NULL)
markf=fopen("electric_circuit.txt","w");
}
fseek(markf,-3,SEEK_END);
fscanf(markf,"%s",check);
if (strcmp(check,"eof"))
{
idm=fopen("id_number.txt","r");

while (1)
{
fscanf(idm,"%s",idmark);
if (!strcmp(idmark,"eof"))
{
fclose(idm);
fprintf(markf,"%s","eof");
fclose(markf);
return 1;
}
printf("enter the mark of student has ID number (%s) :",idmark);
scanf("%f",&mark);
fprintf(markf,"%s\t%f\t\n",idmark,mark);
}
}
else
{
printf("the marks is exist .");
getc(stdin);
getc(stdin);
}
fseek(markf,0,SEEK_END);
int n=ftell(markf);
fclose(markf);
}

You might also like