Skip to content

Instantly share code, notes, and snippets.

@sarikaya
Last active December 23, 2015 23:19
Show Gist options
  • Select an option

  • Save sarikaya/6708891 to your computer and use it in GitHub Desktop.

Select an option

Save sarikaya/6708891 to your computer and use it in GitHub Desktop.
#include "fileoperations.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/****************
Nurefşan Sarıkaya
****************/
using namespace std;
void Dizi::ekle(Tel_Kayit *ykptr){
strcpy(k[kayitsayisi].ad,ykptr->ad);
strcpy(k[kayitsayisi].telno,ykptr->telno);
fseek(teldefteri, 0, SEEK_END);
fwrite(&k[kayitsayisi], sizeof(Tel_Kayit), 1, teldefteri);
kayitsayisi++;
listele(); /*Rehbere yeni kayıt ekler eklemez rehber listesini ekrana alfabetik sıraya göre basar,
kayıt_listele() fonksiyonunu daha fonksiyonel hale getirmek için devre dışı
bırakılabilir.*/
}
void Dizi::olustur(){
kayitsayisi=0;
dosyaadi="teldefteri.txt";
teldefteri = fopen( dosyaadi, "r+" );
if(!teldefteri){
if(!(teldefteri = fopen( dosyaadi, "w+" ))){
cerr << "File can not be opened" << endl;
exit(1);
}
}
}
void Dizi::dosyayi_diziye_aktar(){
Tel_Kayit alayi;
fseek(teldefteri, 0, SEEK_SET);
while(!feof(teldefteri)){
fread(&alayi, sizeof (Tel_Kayit), 1, teldefteri);
if(feof(teldefteri)) break;
strcpy(k[kayitsayisi].ad,alayi.ad);
strcpy(k[kayitsayisi].telno,alayi.telno);
kayitsayisi++;
}
}
void Dizi::kapat(){
fclose(teldefteri);
}
void Dizi::yoket() {
kapat();
if (remove(dosyaadi) != 0) {
cerr << "Error deleting file" << dosyaadi;
}
}
int Dizi::ara(char aranacak[]){
int i=0;
bool tumu=false;
int flag=0;
if(strcmp(aranacak,"*")==0)
tumu=true;
for(int i=0;i<kayitsayisi;i++){
if(!tumu && strcmp(k[i].ad,aranacak)!=0)
continue;
cout<<i+1<<". "<<k[i].ad<<" "<<k[i].telno<<endl;
flag=1;
}
return flag;
}
void Dizi::guncelle(int kayitno, Tel_Kayit *ykptr){
if(fseek(teldefteri, sizeof(Tel_Kayit)*(kayitno-1), SEEK_SET)==0)
fwrite(ykptr, sizeof(Tel_Kayit), 1, teldefteri);
}
void Dizi::dosyayi_duzenle(){
yoket();
teldefteri = fopen( dosyaadi, "r+" );
if(!teldefteri){
if(!(teldefteri = fopen( dosyaadi, "w+" ))){
cerr << "File can not be opened" << endl;
exit(1);
}
}
fseek(teldefteri, 0, SEEK_SET);
// Bos kayitlari atlayarak dosyaya yaz
for(int i=0; i<kayitsayisi; i++) {
if (strncmp(k[i].ad, "", strlen(k[i].ad)) != 0) {
fwrite(&k[i], sizeof(Tel_Kayit), 1, teldefteri);
}
}
kayitsayisi=0;
dosyayi_diziye_aktar();
}
void Dizi::sil(int kayitno){
Tel_Kayit temp;
strcpy(temp.ad, "");
strcpy(temp.telno, "");
int indisno;
indisno=kayitno-1;
k[indisno] = temp;
dosyayi_duzenle();
}
void Dizi::listele(){
Tel_Kayit temp;
int i, j;
for (i=1; i<kayitsayisi; i++)
{
for (j=0; j<kayitsayisi-i; j++)
{
if(strcmp(k[j].ad,k[j+1].ad)>=0)
{
temp = k[j];
k[j] = k[j+1];
k[j+1] = temp;
}
}
}
for(i=0;i<kayitsayisi;i++)
cout<<i+1<<". "<<k[i].ad<<" "<<k[i].telno<<endl;
dosyayi_duzenle();
getchar();
}
#ifndef DOSYAISLEMLERI_H
#define DOSYAISLEMLERI_H
#include <stdio.h>
#include "record.h"
#define MAXKAYIT 100
struct Dizi{
char *dosyaadi;
FILE *teldefteri;
Tel_Kayit k[MAXKAYIT];
void olustur();
void ekle(Tel_Kayit *);
int ara(char []);
void sil(int kayitno);
void guncelle(int kayitno, Tel_Kayit *);
void listele();
int kayitsayisi;
void kapat();
void yoket();
void dosyayi_duzenle();
void dosyayi_diziye_aktar();
};
#endif
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <stdio.h>
#include <ctype.h>
#include "fileoperations.h"
using namespace std;
Dizi defter;
void menu_yazdir();
bool islem_yap(char);
void kayit_ara();
void kayit_ekle();
void kayit_sil();
void kayit_guncelle();
void kayit_listele();
int main(){
defter.olustur();
defter.dosyayi_diziye_aktar();
bool bitir = false;
char secim;
while (!bitir) {
menu_yazdir();
cin >> secim;
bitir = islem_yap(secim);
}
defter.kapat();
return EXIT_SUCCESS;
}
void menu_yazdir(){
system("cls");
cout << endl << endl;
cout << "Phone Book Application - Nurefsan Sarikaya" << endl;
cout << "Choose an option" << endl;
cout << "A: Search Record" << endl;
cout << "E: Add Record" << endl;
cout << "G: Update Record" << endl;
cout << "S: Delete Record" << endl;
cout << "L: Listele" << endl;
cout << "C: Exit" << endl;
cout << endl;
cout << "Enter your option {A, E, G, S, L, C} : ";
}
bool islem_yap(char secim){
bool sonlandir=false;
switch (secim) {
case 'A': case 'a':
kayit_ara();
break;
case 'E': case 'e':
kayit_ekle();
break;
case 'G': case 'g':
kayit_guncelle();
break;
case 'S': case 's':
kayit_sil();
break;
case 'L': case 'l':
kayit_listele();
break;
case 'C': case 'c':
cout << "Are you sure that you want to terminate the program? (E/H):";
cin >> secim;
if(secim=='E' || secim=='e')
sonlandir=true;
break;
default:
cout << "Error: You have made an invalid choice" << endl;
cout << "Try again {A, E, G, S, C} :" ;
cin >> secim;
sonlandir = islem_yap(secim);
break;
}
return sonlandir;
}
void kayit_ara(){
char ad[AD_UZUNLUK];
cout << "Please enter the name of the person you want to search (press '*' for listing all):" << endl;
cin.ignore(1000, '\n');
cin.getline(ad,AD_UZUNLUK);
if(defter.ara(ad)==0){
cout << "Record can not be found" << endl;
}
getchar();
};
void kayit_ekle(){
Tel_Kayit yenikayit;
cout << "Please enter the information of the person you want to save " << endl;
cout << "Name : " ;
cin.ignore(1000, '\n');
cin.getline(yenikayit.ad,AD_UZUNLUK);
cout << "Phone number :";
cin >> setw(TELNO_UZUNLUK) >> yenikayit.telno;
defter.ekle(&yenikayit);
cout << "Record has been added" << endl;
getchar();
};
void kayit_sil(){
char ad[AD_UZUNLUK];
int secim;
cout << "Please enter the name of the person you want to delete (press '*' for listing all):" << endl;
cin.ignore(1000, '\n');
cin.getline(ad,AD_UZUNLUK);
int kisisayisi=defter.ara(ad);
if(kisisayisi==0){
cout << "Record can not be found" << endl;
}
else {
if (kisisayisi==1){
cout << "Record has been found." << endl;
cout << "Please enter the index of the record if you want to delete this contact (Press -1 to exit without deletion): " ;
}
else
cout << "Please enter the index of the record that you want to delete (Press -1 to exit without deletion): " ;
cin >> secim;
if(secim==-1) return;
defter.sil(secim);
cout << "Record has been deleted" <<endl;
}
getchar();
};
void kayit_guncelle(){
char ad[AD_UZUNLUK];
int secim;
cout << "Please enter the name of the person you want to update (press '*' for listing all):" << endl;
cin.ignore(1000, '\n');
cin.getline(ad,AD_UZUNLUK);
int kisisayisi=defter.ara(ad);
if(kisisayisi==0){
cout << "Record can not be found" << endl;
}
else {
if (kisisayisi==1){
cout << "Record has been found." << endl;
cout << "Please enter the index of the record if you want to update this contact (Press -1 to exit without updating) " ;
}
else
cout << "Please enter the index of the record that you want to update (Press -1 to exit without updating): " ;
cin >> secim;
if(secim==-1) return;
Tel_Kayit yenikayit;
cout << "Please enter the up-to-date information" << endl;
cout << "Name : " ;
cin.ignore(1000, '\n');
cin.getline(yenikayit.ad,AD_UZUNLUK);
cout << "Phone number :";
cin >> setw(TELNO_UZUNLUK) >> yenikayit.telno;
defter.guncelle(secim,&yenikayit);
cout << "Record has been updated successfully" <<endl;
}
getchar();
};
void kayit_listele(){
defter.listele();
getchar();
}
#define AD_UZUNLUK 30
#define TELNO_UZUNLUK 15
struct Tel_Kayit{
char ad[AD_UZUNLUK];
char telno[TELNO_UZUNLUK];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment