I edited this post cause I found a stupid mistake in my person class.
This is my program. not all functions have been implemnted.
Don't take this thing very seriously, since I have abandoned the same project tons of times due I/O problems. Whenever theres a cin any where in the program before a cin.getline() it is ignored by the program, ie, the program does not wait for input. also the name is not copied on to the object; only the number is copied.
don't know why.
You were right; binary isn't my cup o' tea.
The class.
Code:
class Person{
private:
char name[30], numb[15];
public:
Person(){
name[0] = '\0';
numb[0] = '\0';
}
char* get_name(){ return name; }
char* get_numb(){ return numb; }
void set(char*, char*);
};
void Person::set(char* na, char* n1){
for(int i=0; na[ i] !='\0'; i++)name[ i] = na[ i]; //This was edited and the next line added.
name[ i] = '\0';
for(i=0; n1[ i] !='\0'; i++)numb[ i] = n1[ i];
numb[ i] = '\0';
}
program.
Code:
//Note the size of buffer used to read all the records.
//It might need expansion or some thing.
//deletion of entries not implemented.
//edit of entries not implemented.
const char *substr(const char *match, const char *s); //remember me?
void list_record(Person*, int);
void search_record(Person*, int);
void add_record(Person*, int&);
void delete_record(Person*, int&);
void edit_record(Person*, int);
void refresh_file(Person*, int, fstream &);
long read_from_file(Person*, fstream &); //transfer all records to Person*.
int main(){
clrscr();
fstream myfile;
myfile.open("Numbers.imp", ios::in | ios::binary | ios::app);
if(myfile.fail())cout<<"E R R O R Opening File !\n";
else{
Person buffer[30];
int size = read_from_file(buffer, myfile); //every thing is in buffer.
int n = size/sizeof(Person);
char ch;
cout<<"No: of saved records :"<<n<<endl;
myfile.close(); //Let's close it for now...
do{
cout<<"\n<< M E N U >>\n"
<<"1) List ('l')"<<endl
<<"2) Search ('s')"<<endl
<<"3) Add New Entry ('n')"<<endl
<<"4) Delete an entry ('d'){not implemented yet}"<<endl
<<"5) Edit an entry ('e') {n i y}"<<endl
<<"6) Quit ('q')"<<endl
<<"Your Choice :";
cin>>ch; //I think this cin causes cin.getline() problems.
//cause when I changed it to getche(), the program waited for input
//but name was'nt copied to object still.
switch(ch){
case 'l':list_record(buffer, n);
break;
case 's':search_record(buffer, n);
break;
case 'n':add_record(buffer, n);
break;
case 'd':delete_record(buffer, n);
break;
case 'e':edit_record(buffer, n);
break;
case 'q':refresh_file(buffer, n, myfile);
cout<<"Records updated...\n\n\n"
<<"Exiting...\n\n\nPress any key...";
getch();
exit(0);
default :cout<<"Invalid Choice !!!\n";
}
}while(1);
}
return 0;
}
//Is there a file size function???below
long read_from_file(Person* buff, fstream &file){
file.seekg(0, ios::beg); file.seekp(0, ios::beg);
long start = file.tellg(), end, size;
file.seekg(0, ios::end);
end = file.tellg();
size = end-start;
file.seekg(0, ios::beg);
file.read((unsigned char*) &buff, size); //Copies records to buffer.
return size;
}
void list_record(Person* buff, int n){
if(n){
cout<<endl;
for(int i=0; i<n; i++)
cout<<i+1<<") "<<buff[ i].get_name()<<"\t\t\t"<<buff[ i].get_numb()<<endl;
for(i=0; i<20; i++)cout<<"==";
cout<<endl;
}
}
void search_record(Person* buff, int n){
char na[30];
cout<<"Enter the name or a part of the name :";
cin.getline(na, 30); //This isn't working...
for(int i=0; i<n; i++)
if(substr(na, buff[ i].get_name()) )
cout<<i+1<<") "<<buff[ i].get_name()<<"\t\t\t"<<buff[ i].get_numb()<<endl;
for(i=0; i<20; i++)cout<<"==";
cout<<endl;
}
void add_record(Person* buff, int& n){
char name[30], number[15];
cout<<"Enter the name :";
cin.getline(name, 30); //This isn't working... program not waiting for input.
cout<<"Enter the number :"; //The program jumps to this line instead
cin.getline(number, 15); //But here the machine DOES wait for input.
//I think the problem is in the 'cin>>ch;' in the
//previous 'switch'. but dont know why.
Person a;
a.set(name, number);
buff[ n]=a;
n++;
cout<<"Record added :"<<endl;
}