Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define max 3
struct og
{
char id[12];
char tc[11];
char name[30];
char tel[11];
char adres[50];
}ogrenci[max],ogr[max],ex;
FILE *kp;
void menu(void); // menu
void add(void); // add user
void del(void); // delete
void list(void); // list of users
void uptade(void); //uptade the information
void main()
{
menu();
system("pause");
}
void menu()
{
char secim;
system("cls");
menu:
printf("\n\n\n\t########MAIN MENU########\n\n");
printf("\n\t[A]-ADD NEW STUDENT\n");
printf("\t[D]-DELETE\n");
printf("\t[U]-UPTADE\n");
printf("\t[L]-LIST\n");
printf("\t[Q]-QUIT\n");
printf("\t\n\n\n[PLEASE SELECT]\n");
scanf("%c",&secim);
printf("\n");
while( getchar() != '\n' ); // gets i etkilememesi için kullandık
switch(secim)
{
case 'a': add(); break;
case 'A': add(); break;
case 'd': del(); break;
case 'D': del(); break;
case 'u': uptade(); break;
case 'U': uptade(); break;
case 'l': list(); break;
case 'L': list(); break;
case 'q': exit(0); break;
case 'Q': exit(0); break;
default: system("cls"); goto menu; break;
}
}
/*******************************************/
/********KAYIT FORMU***********************/
void add()
{
int ret;
int i;
int j;
int k;
char temp[50];
system("cls");
if((kp=fopen("kayit.txt","a+"))==NULL)
{
printf("hata\n\n");
} //if
else
{
i=0;
do
{
printf("enter id>\n");
gets(temp);
strcpy(ogrenci[i].id,temp);
for(j=0; j<max; j++)
{
if(i==j)
{
continue;
}
else if(!strcmp(ogrenci[i].id,ogrenci[j].id))
{
printf("\nBu ID kullanilmaktadir!\n");
printf("enter id>\n");
gets(temp);
strcpy(ogrenci[i].id,temp);
break;
}
}// for j
printf("\n");
printf("enter tc no>\n");
gets(temp);
strcpy(ogrenci[i].tc,temp);
for(j=0; j<max; j++)
{
if(j==i)
{
continue;
} //if
else if(!strcmp(ogrenci[i].tc,ogrenci[j].tc))
{
printf("\nBu TC NO kullanilmaktadir!\n");
printf("enter tc no>\n");
gets(temp);
strcpy(ogrenci[i].tc,temp);
break;
} // if
}// for j
printf("\nenter name>\n");
gets(temp);
strcpy(ogrenci[i].name,temp);
printf("\nenter tel no>\n");
gets(temp);
strcpy(ogrenci[i].tel,temp);
printf("\nenter addresses>\n");
gets(temp);
strcpy(ogrenci[i].adres,temp);
printf("\n");
i++;
}while(i<max);
for(i=0; i<max; i++)
{
ret=fwrite(&ogrenci[i],sizeof(ex),1,kp);
}
}//else
fclose(kp);
menu();
}//add
void del()
{
int i,j,ret,a;
char id[12];
if((kp=fopen("kayit.txt","r+"))==NULL)
{
printf("can not open\n");
exit(0);
}//if
else
{
for(i=0; i<max; i++)
{
ret=fread(&ogrenci[i],sizeof(ex),1,kp);
}//for
printf("enter id for deleting>\n");
gets(id);
for(i=0; i<max; i++)
{
if(!strcmp(id,ogrenci[i].id))
{
a=i;
break;
}//if
}//for
ogrenci[a].adres[0]='\0';
ogrenci[a].id[0]='\0';
ogrenci[a].tc[0]='\0';
ogrenci[a].tel[0]='\0';
ogrenci[a].name[0]='\0';
printf("deleted...\n");
for(i=0; i<max; i++)
{
ret=fwrite(&ogrenci[i],sizeof(ex),1,kp);
}
fclose(kp);
}//else
}
void uptade()
{
char ch;
char id[12];
char tc[11];
char name[30];
char tel[11];
char adres[50];
int i,k,a;
int ret;
system("cls");
if((kp=fopen("kayit.txt","r+"))==NULL)
{
printf("can not open\n");
exit(0);
}//if
for(i=0; i<max; i++)
{
ret=fread(&ogrenci[i],sizeof(ex),1,kp);
}//for
printf("enter ID for updating>\n");
gets(id);
for(i=0; i<max; i++)
{
if(!strcmp(id,ogrenci[i].id))
{
break;
}//if
}//for
select:
printf("what do you want to update\n\n");
printf("[i]-id\n");
printf("[t]-tc no\n");
printf("[n]-name\n");
printf("[o]-tel no\n");
printf("[a]-addresses\n");
scanf("%c",&ch);
printf("\n");
while( getchar() != '\n' );
switch(ch)
{
case 'i':
printf("enter new id>\n");
gets(id);
k=strlen(id);
ogrenci[i].id[0]='\0';
for(a=0; a<k; a++)
{
ogrenci[i].id[a]=id[a];
}
break;
case 'I':
printf("enter new id>\n");
gets(id);
strcpy(ogrenci[i].id,id);
break;
case 't':
printf("enter new tc no>\n");
gets(tc);
strcpy(ogrenci[i].tc,tc);
break;
case 'T':
printf("enter new tc no>\n");
gets(tc);
strcpy(ogrenci[i].tc,tc);
break;
case 'n':
printf("enter new name>\n");
gets(name);
strcpy(ogr[i].name,name);
break;
case 'N':
printf("enter new name>\n");
gets(name);
strcpy(ogrenci[i].name,name);
break;
case 'o':
printf("enter new tel no>\n");
gets(tel);
strcpy(ogrenci[i].tel,tel);
break;
case 'O':
printf("enter new tel no>\n");
gets(tel);
strcpy(ogrenci[i].tel,tel);
break;
case 'a':
printf("enter new addresses>\n");
gets(adres);
strcpy(ogrenci[i].adres,adres);
break;
case 'A':
printf("enter new addresses>\n");
gets(adres);
strcpy(ogrenci[i].adres,adres);
break;
default: printf("error\n"); goto select; break;
}//switch
for(i=0; i<max; i++)
{
ret=fwrite(&ogrenci[i],sizeof(ex),1,kp);
}
fclose(kp);
printf("kaydedildi....\n");
system("pause");
menu();
}
void list()
{
int i,j,ret;
if((kp=fopen("kayit.txt","r"))==NULL)
{
printf("can not open\n\n");
}//if
for(i=0; i<max; i++)
{
ret=fread(&ogr[i],sizeof(ex),1,kp);
}
printf("\n\n-----LIST OF STUDENTS-----\n");
for(i=0; i<max+3; i++)
{
printf("ID: %s\n",ogr[i].id);
printf("TC NO: %s\n",ogr[i].tc);
printf("NAME: %s\n",ogr[i].name);
printf("TEL NO: %s\n",ogr[i].tel);
printf("ADDRESSES: %s\n",ogr[i].adres);
printf("\n");
}//for
fclose(kp);
system("pause");
menu();
}