#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; 
}

>> searching does not work pls help

You should be much more specific as to what is not working. You might read Narue's Read This Before Posting - especially the section Describe your problem clearly and fully!.

it was so clear. nonetheless
pls run the program.
add file..... name:burcin surname:erek number:11111
after that switch to search section
write burcin.... the program could not find it.
why even though i write "burc" program finds the record.
but does not work

>> was so clear. nonetheless

Not to me, at least. Generally, just stating that something "does not work" is too vague and is likely to decrease the number of people who are willing to have a closer look at a given problem.

But that aside, what happens when you write to the file, i.e.

file<<name<<""<<surname<<""<<no<<"\n";

Does that match with how you are later reading the file's content? If you open the student.txt in an editor, you'll probably figure out what's wrong.

Some things..

  • In C++, calling the function main() is strictly illegal (a nice do/while() would be much better).
  • Where do you have MAX_PATH defined? When dealing with files/paths you might use FILENAME_MAX from <cstdio>.
  • You should #include <string>, in order to do "getline(file,line)" in standard-compliant manner.
  • You should #include <cstring>, in order to use strcmp() in standard-compliant manner.
  • Instead of #include<stdlib.h>, do #include<cstdlib>.
  • You might find alternatives to <conio.h>/getch().
  • Sooner or later, using .eof() in a loop control will bite you!
  • You could drop the raw character arrays altogether and use std::string instead, that way you would not risk buffer overruns like you do now. Otherwise, rather specify the buffer size like so
#include <iomanip>
...
char buffer[SOME_SIZE_HERE] = "";
cin >> setw(sizeof(buffer)) >> buffer;

i open student.txt saw that there is no space between them so search function does not find record and not count

file<<name<<" "<<surname<<" "<<no<<"\n";
if i put space between them during file read , it helps me for the search.
thanks

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.