hello everybody...
i am trying to make a project on binary files and that needs a function to modify data in a binary file. I have made the following class.
class example
{
public:
char name[10], number[5];
void getdetails();
void showdetails();
example()
{
name[0]='\0', number[0]='\0';
}
char* getname()
{
return name;
}
};
and the definition of mod_data() ( a function to modify data of the class ) is as follows:
void mod_data(example &abc)
{
char name[10], number;
cout<<"NAME: ";
gets(name);
if(strlen(name)!=0)
strcpy(abc.name, name);
cout<<"NUMBER: ";
gets(number);
if(strlen(number)!=0)
strcpy(abc.number, number);
}
to modify the record in the binary file, function modify is used
void modify()
{
int recc = 0;
int flag=0;
char name[25];
example abc;
clrscr();
fstream fil ("EXAMPLE.dat",ios::in);
cout<<"\n Enter The Name Of The Person Whose Details To Be Modified: ";
gets(name);
cout<<endl;
while (!fil.eof())
{
fil.read ((char*)&abc,sizeof(abc));
recc++;
if (stricmp(abc.getname(), name)==0)
{
flag=1;
break;
}
}
fil.close();
if(flag==1)
{
fil.open ("EXAMPLE.dat",ios::out|ios::ate);
mod_data(abc);
fil.seekp((recc-1)*sizeof(abc), ios::beg);
fil.write((char*)&abc,sizeof(abc));
fil.close();
}
else
{
cout<<"\n PERSON NOT FOUND";
getch();
}
}
but the program is not working properly.
in my original class there are more than 15 data members. And what i want to do is if the user presses enter in a particular field the record for that field which was present before remains as it is i.e. that field is not modified. But the function works properly for only the first few data members and not for the last ones.The last data members when displayed appear blank or garbage data is displayed. Please help me out.