C Application (File Operations )
This tutorial shows us that how to read from txt file , storing in the array structer and searching from that array structer.For this tutorial you need to create a txt file named phonebook and write name , surname and number like this .
Garen Haddeler 87143232141
Ali Emre 231241421
Name Surname 23123124124
.....................................................
You all succed!!
mail me if you have question or suggestion
mail:g.haddeler@gmail.com
C Codes :
----------------------------------
#include <stdio.h>
#include <string.h>
#include <conio.h>
// Program creates an array structer and stores the chars that taken from phonebook.txt file .
//It has two other function that searching name and last name individually
struct phonebook{
char name[16];
char surname[21];
char num[11];
};
int name_searching(char *FirstName, int size );
int lastname_searching(char *LastName, int size);
struct phonebook c[30];
FILE *ptVeri;
int main(){
int secim=0;
char searchname[20];
int NUM ;
if((ptVeri=fopen("phonebook.txt","r"))==NULL){
printf("\n ERROR couldnt open txt file !!! ");
}
int i =0;
while(!feof(ptVeri)){
fscanf(ptVeri,"%s%s%s",&c[i].name, &c[i].surname,&c[i].num);
i++ ;}
fclose(ptVeri);
while(1){
printf("Searching for First Name enter 1 \n");
printf("Searching for Last Name enter 2 \n");
printf("Exit for enter 3 \n");
scanf("%d",&secim);
switch (secim){
case 1:
printf("\nWrite first name > (max 20 character):");
scanf("%s",searchname);
NUM = name_searching(searchname,i);
if ( NUM == 0){ // geri bildirim:
printf("\n NOT FOUND \n ");
}else {
if( NUM > 0)
printf("\nTotal %d search is found .\n", NUM );
else
printf("\n ERROR !!! ");
}
break;
case 2:
printf("\nWrite last name > (max 20 character):");
scanf("%s",searchname);
NUM = lastname_searching(searchname,i);
if ( NUM == 0){ // geri bildirim:
printf("\n NOT FOUND ");
}else {
if( NUM > 0)
printf("\nTotal %d search is found .\n ", NUM );
else
printf("\n ERROR !!!");
}
break;
case 3 : break;
default : printf("Only enter 1-2-3"); break;
}
if(secim==3) break;
}
}
int name_searching(char *FirstName, int size ){
int count=0;
for( int d =0 ; d<size ; d++){
if(!strcmp(FirstName, c[d].name)){
printf("%s",c[d].name);
printf(" %s",c[d].surname);
printf(" %s\n",c[d].num) ;
count++;
}}
return count;
}
int lastname_searching(char *LastName, int size ){
int count=0;
for( int d =0 ; d<size ; d++){
if(!strcmp(LastName, c[d].surname)){
printf("%s",c[d].name);
printf(" %s",c[d].surname);
printf(" %s\n",c[d].num) ;
count++;
}}
return count;
}
----------------------------------------------
Garen Haddeler 87143232141
Ali Emre 231241421
Name Surname 23123124124
.....................................................
You all succed!!
mail me if you have question or suggestion
mail:g.haddeler@gmail.com
C Codes :
----------------------------------
#include <stdio.h>
#include <string.h>
#include <conio.h>
// Program creates an array structer and stores the chars that taken from phonebook.txt file .
//It has two other function that searching name and last name individually
struct phonebook{
char name[16];
char surname[21];
char num[11];
};
int name_searching(char *FirstName, int size );
int lastname_searching(char *LastName, int size);
struct phonebook c[30];
FILE *ptVeri;
int main(){
int secim=0;
char searchname[20];
int NUM ;
if((ptVeri=fopen("phonebook.txt","r"))==NULL){
printf("\n ERROR couldnt open txt file !!! ");
}
int i =0;
while(!feof(ptVeri)){
fscanf(ptVeri,"%s%s%s",&c[i].name, &c[i].surname,&c[i].num);
i++ ;}
fclose(ptVeri);
while(1){
printf("Searching for First Name enter 1 \n");
printf("Searching for Last Name enter 2 \n");
printf("Exit for enter 3 \n");
scanf("%d",&secim);
switch (secim){
case 1:
printf("\nWrite first name > (max 20 character):");
scanf("%s",searchname);
NUM = name_searching(searchname,i);
if ( NUM == 0){ // geri bildirim:
printf("\n NOT FOUND \n ");
}else {
if( NUM > 0)
printf("\nTotal %d search is found .\n", NUM );
else
printf("\n ERROR !!! ");
}
break;
case 2:
printf("\nWrite last name > (max 20 character):");
scanf("%s",searchname);
NUM = lastname_searching(searchname,i);
if ( NUM == 0){ // geri bildirim:
printf("\n NOT FOUND ");
}else {
if( NUM > 0)
printf("\nTotal %d search is found .\n ", NUM );
else
printf("\n ERROR !!!");
}
break;
case 3 : break;
default : printf("Only enter 1-2-3"); break;
}
if(secim==3) break;
}
}
int name_searching(char *FirstName, int size ){
int count=0;
for( int d =0 ; d<size ; d++){
if(!strcmp(FirstName, c[d].name)){
printf("%s",c[d].name);
printf(" %s",c[d].surname);
printf(" %s\n",c[d].num) ;
count++;
}}
return count;
}
int lastname_searching(char *LastName, int size ){
int count=0;
for( int d =0 ; d<size ; d++){
if(!strcmp(LastName, c[d].surname)){
printf("%s",c[d].name);
printf(" %s",c[d].surname);
printf(" %s\n",c[d].num) ;
count++;
}}
return count;
}
----------------------------------------------
Yorumlar
Yorum Gönder