#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void write();
void rear();
void search();
void write()
{
int no;
char name[80],surname[80];
ofstream file("student.txt",ios::out|ios::app);
cout<<"name:";
cin>>name;
cout<<"surname :";
cin>>surname;
cout<<"number :";
cin>>no;
file<<name<<""<<surname<<""<<no<<"\n";
file.close();
}
void read()
{
string line;
ifstream file("student.txt",ios::in);
if(!file){cout<<"file not found.\n"; getch(); exit(1);}
while(!file.eof())
{
getline(file,line);
cout<<line<<endl;
}
file.close();
}
void search()
{
char word[MAX_PATH]={0},str[MAX_PATH]={0};
string fname;
int sayac=0;
ifstream dosyaoku("student.txt",ios::in);//file name to be searched
if(!dosyaoku){cout<<"file not found.\n"; getch(); exit(1);}
cout<<"enter the word for search :";
cin>>word;
while(!dosyaoku.eof())
{
dosyaoku>>str;
if(strcmp(word,str)==0) { sayac++;}
else{}
}
if(sayac==0) {cout<<"bulunmadi"<<endl;}
else {cout<<sayac<<" tane"<<" bulundu";}
dosyaoku.close();
}
int main ()
{
int opt; char c;
system("cls");
cout<<"------MENU-------\n";
cout<<"1-) adding file\n";
cout<<"2-) reading file\n";
cout<<"3-) search \n";
cin>>opt;
switch(opt)
{
case 1: write(); break;
case 2: read(); break;
case 3: search(); break;
default: cout<<"wrong input\n";
}
cout<<"\ncontinue(y/n)?";
cin>>c;
if(c=='y') {return main();}
getch();
return 0;
}
burcin erek 0 Junior Poster in Training
mitrmkar 1,056 Posting Virtuoso
burcin erek 0 Junior Poster in Training
mitrmkar 1,056 Posting Virtuoso
burcin erek 0 Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.