i have some code in the main() function that reads the data and prints it onscreen by the function putdata(). also i have seen the raw file. the data is not updated.
the full code is here:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<process.h>
/* Abhishek Dey Das */
/* Telephone Directory Program */
/* Program is under construction and
contains bugs */
char file[20]={NULL}; //global variable for filename
int id_no; //global variable for ID
class add {
public:
char adress[50], nickname[10], email[30];
char pn[50];
char city[10], name[30];
void putdata(); //function to display records
void addrecord(); //function to enter records
}; //end of class add
class operation
{
add z;
fstream a;
int pos,loc;
public:
operation()
{pos=0;}
void search_byname(); //search by name
void search_bycity(); //search by city
void search_bypn(); //search by phone number
void modify_record(); //modify record
}; //end of class operation
void add::putdata()
{
textcolor(8);
clrscr();
gotoxy(1,17);
cout<<endl<<" Record No : "<<id_no;
cout<<endl<<"\n First Name: "<<name;
cout<<endl<<"\n Surname : "<<nickname;
cout<<endl<<"\n Address : "<<adress;
cout<<endl<<"\n City : "<<city;
cout<<endl<<"\n Phone no. : "<<pn;
cout<<endl<<"\n Email : "<<email;
getch();
} //end of putdata() of class add
void add::addrecord()
{
textcolor(4);
clrscr();
gotoxy(1,10);
textcolor(CYAN);
//inputting data for a record
cprintf("ADD NEW RECORDS");
cout<<"\n";
cprintf("--- --- -------");
cout<<"\n Enter the first name : ";
gets(name);
cout<<"\n Enter the surname : ";
gets(nickname);
cout<<"\n Enter the address : ";
gets(adress);
cout<<"\n Enter the City : ";
gets(city);
cout<<"\n Phone number : ";
gets(pn);
cout<<"\n Enter the email : ";
gets(email);
} //end of addrecord() of class add
void operation::modify_record()
{
//code for modify function
fstream b;
int k=0;
b.open("temp.dat",ios::out|ios::binary);
a.open(file,ios::in|ios::binary);
while(a)
{
k++;
if(pos!=k)
{
a.read((char*)&z,sizeof(z));
b.write((char*)&z,sizeof(z));
}
else
{
z.addrecord();
b.write((char*)&z,sizeof(z));
}
}
a.close();
b.close();
//Transferring the new contents
a.open(file,ios::out|ios::binary);
b.open("temp.dat",ios::in|ios::binary);
while(b)
{
b.read((char*)&z,sizeof(z));
a.write((char*)&z,sizeof(z));
}
a.close();
b.close();
delete "temp.dat";
} //end of modify_record() of class operation
void operation::search_byname()
{
//Code for searching by name
clrscr();
int ch1;
char naam[30];
int flag=0;
char x;
int rag;
int j=0;
id_no=0;
pos=0;
textcolor(CYAN);
gotoxy(1,10);
cprintf("\n\n SEARCH BY NAME ");
cout<<"\n\n 1. Search by first name";
cout<<"\n 2. Search by surname";
cout<<"\n\n";
cin>>ch1;
if (ch1==1)
{
clrscr();
char x;
gotoxy(1,10);
textcolor(CYAN);
cprintf("\n\n SEARCH BY NAME ");
cout<<"\n\n Enter name to search : ";
gets(naam);
int n=strlen(naam);
a.open(file,ios::in|ios::binary);
a.read((char*)&z,sizeof(z));
while(a)
{
id_no++;
pos++;
for (int j=0; j<n; j++)
{
if (z.name[j]!=naam[j])
{ flag=0;
break;
}
else
{
flag=1;
rag=10;
}
}
if (flag==1)
{
z.putdata();
cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
cin>>x;
if('Y'==toupper(x))
{
modify_record();
break;
}
}
a.read((char*)&z,sizeof(z));
}
a.close();
loc=0;
}
else
{
clrscr();
gotoxy(1,10);
textcolor(CYAN);
cprintf("\n\n SEARCH BY SURNAME ");
cout<<"\n\n Enter surname to search : ";
gets(naam);
int n=strlen(naam);
a.open(file,ios::in|ios::binary);
a.read((char*)&z,sizeof(z));
while(a)
{
id_no++;
pos++;
for (int j=0; j<n; j++)
{
if (z.nickname[j]!=naam[j])
{ flag=0;
break;
}
else
{
flag=1;
rag=10;
}
}
if (flag==1)
{
z.putdata();
cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
cin>>x;
if('Y'==toupper(x))
{
modify_record();
break;
}
}
a.read((char*)&z,sizeof(z));
id_no++;
}
a.close();
loc=0;
}
if (flag==0&&rag!=10) //control is not going over here
{
cout<<flag;
textcolor(13+128);
cprintf("\n\n Record Not Found!");
}
getch();
};
void operation::search_bycity()
{
//Code for searching by name
clrscr();
int ch1;
char city[30];
int flag=0;
int rag;
int j=0;
id_no=0;
char x;
pos=0;
textcolor(CYAN);
gotoxy(1,10);
textcolor(CYAN);
cprintf("\n\n SEARCH BY CITY ");
cout<<"\n\n Enter city to search : ";
gets(city);
int n=strlen(city);
a.open(file,ios::in|ios::binary);
a.read((char*)&z,sizeof(z));
while(a)
{
id_no++;
pos++;
for (int j=0; j<n; j++)
{
if (z.city[j]!=city[j])
{ flag=0;
break;
}
else
{
flag=1;
rag=10;
}
}
if (flag==1)
{
z.putdata();
cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
cin>>x;
if('Y'==toupper(x))
{
modify_record();
break;
}
}
a.read((char*)&z,sizeof(z));
}
loc=0;
a.close();
if (flag==0&&rag!=10) //control is not going over here
{
cout<<flag;
textcolor(13+128);
cprintf("\n\n Record Not Found!");
}
getch();
}; //end of search_bycity() of class operation
void operation::search_bypn()
{
//Code for searching by telephone number
clrscr();
int ch1;
char pn[50];
int flag=0;
int rag;
int j=0;
id_no=0;
pos=0;
char x;
textcolor(CYAN);
gotoxy(1,10);
textcolor(CYAN);
cprintf("\n\n SEARCH BY TELEPHONE NUMBER ");
cout<<"\n\n Enter number to search : ";
cin>>pn;
int n=strlen(pn);
a.open(file,ios::in|ios::binary);
a.read((char*)&z,sizeof(z));
while(a)
{
id_no++;
pos++;
for (int j=0; j<n; j++)
{
if (z.pn[j]!=pn[j])
{ flag=0;
break;
}
else
{
flag=1;
rag=10;
}
}
if (flag==1)
{
z.putdata();
cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
cin>>x;
if('Y'==toupper(x))
{
modify_record();
break;
}
}
a.read((char*)&z,sizeof(z));
}
loc=0;
a.close();
if (flag==0&&rag!=10) //control is not going over here
{
cout<<flag;
textcolor(13+128);
cprintf("\n\n Record Not Found!");
}
getch();
};
void main()
{
fstream a;
add b;
operation o;
int t;
do
{
textcolor(2);
clrscr();
gotoxy(1,10);
cout<<endl<<" PERSONAL TELEPHONE DIRECTORY";
cout<<endl<<" By Abhishek Dey Das \n\n";
cout<<endl<<" 1:open";
cout<<endl<<" 2:create";
cout<<endl<<" 3:Add record";
cout<<endl<<" 4:search";
cout<<endl<<" 5:display";
cout<<endl<<" 6:close File";
cout<<endl<<" 7:exit\n";
textcolor(RED);
cprintf("\n HELP: ");
cout<<"If you are a new user then create a new file using Option 2";
cout<<endl<<"\t\tIf you are not a new user and have just logged in, then";
cout<<endl<<"\t\topen the desired file using Option 1 \n\n";
cout<<endl<<"COMMAND :> ";
cin>>t;
switch(t)
{
case 1: cout<<"\n\nEnter the file name: ";
cin>>file;
break;
case 2: cout<<"\n\nEnter the file name: ";
cin>>file;
a.open(file,ios::out|ios::trunc);
cout<<"\nYour file has been created with the name: "<<file;
getch();
a.close();
break;
case 3: a.open(file,ios::app|ios::binary);
char y;
do
{
a.seekg(0);
b.addrecord();
a.write((char*)&b,sizeof(b));
cout<<"Do you want to add one more record?[y/n]: ";
cin>>y;
} while('Y'==toupper(y));
a.close();
break;
case 4: int x;
textcolor(11);
clrscr();
gotoxy(1,10);
cout<<"\n\n 1.by Name";
cout<<"\n\n 2.by City";
cout<<"\n\n 3.by phone number";
cout<<"\n\n\n\n -------->";
cin>>x;
switch(x)
{
case 1: o.search_byname(); break;
case 2: o.search_bycity(); break;
case 3: o.search_bypn(); break;
}
break;
case 5: id_no=0;
a.open(file,ios::in,ios::binary);
a.seekg(0);
a.read((char*)&b,sizeof(b));
while(a.eof()==0)
{
id_no++;
b.putdata();
a.read((char*)&b,sizeof(b));
}
a.close();
break;
case 6: *file=NULL; break;
case 7: exit(0);
default: cout<<"\n\n Invalid Entry!"; break;
}
}while(t<9);
}
i understand the consequences of using classes like that but Turbo C++ 3.0 is too ancient for that.
:
This message was edited by stober at 2006-1-26 11:32:15
: : sorry about not asking the question :). my question was that the function modify_record() is not working. i added the ios::binary() but it didn't work. the code is:
:
: what makes you think it doesn't work? Do you have another function that re-reads the record?
:
: And trying to write a c++ class like that is not a good idea except in the very simplest cases. That will not work if the class contains other c++ containers, such as std::string, or instances of other c++ classes, or base class that contains c++ containers, because that data is not part of the c++ class instance. You should allow each class (and base class(s)) to write out its own data.
:
:
:
Thank you.
Abhishek
http://freepgs.com/abhishek