I have a problem working with files in c++.This is my code for modifying a record in a file.When i execute it ,it doesn't check rec in file & comes out .What's wrong???
Many time working with file(adding record &then printing on screen or after deletion print the rec )last rec is read twice or otherwise each rec ie read twice I have checked my code.Still it's not working Pls help me If u can give correct code it will help me.
[Code=cpp]
#include<fstrea m.h>
#include<conio. h>
#include<stdio. h>
#include<string .h>
#include<proces s.h>
class emp
{
int id;
char name[20];
float sal;
public:
void getdata()
{
cout<<"Enter id:="<<"\n";
cin>>id;
cout<<"Enter name:="<<"\n";
cin>>name;
cout<<"Enter salary:="<<"\n" ;
cin>>sal;
}
void putdata()
{
cout<<"Id :="<<"\t "<<id<<endl ;
cout<<"Name :="<<"\t "<<name<<en dl;
cout<<"Salary :="<<"\t "<<sal<<end l;
}
int getid()
{
return id;
}
void modify();
}e1,emp1;
void emp::modify()
{
cout<<"Emp id" <<id<<"\t";
cout<<"Emp name" <<name<<"\t";
cout<<"Emp Salary" <<sal<<"\n";
cout<<"Enter new details "<<endl;
char nm[20]=" ";
int sal1;
cout<<"New Name :(enter '.' to retain old one)";
cin>>nm;
cout<<"New Salary :(enter '-1' to retain old one)";
cin>>sal1;
if(strcmp(nm,". ")!=0)
strcpy(name,nm) ;
if(sal1!=-1)
sal=sal1;
}
void main()
{
long pos;
char found='f';
int id1;
clrscr();
fstream fio;
fio.open("emp.d at",ios::app|io s::out|ios::bin ary);
emp1.getdata();
fio.write((char *)&emp1,sizeof (emp1));
cout<<"The contents before modification \n";
while(!fio.eof( ))
{
fio.read((char *)&emp1,sizeof (emp1));
emp1.putdata();
}
cout<<"Enter id of an Emploee whose record is to be modified "<<endl;
cin>>id1;
fio.seekg(0);
while(fio)
{
pos=fio.tellg() ;
fio.read((char *)&e1,sizeof (e1));
if(e1.getid()== id1)
{
e1.modify();
fio.seekg(pos);
fio.write((char *)&e1,sizeof(e1 ));
found='t';
break;
}
}
if(found=='f')
cout<<"Record not found !!\n";
fio.seekg(0);
cout<<"Now the contents \n";
while(!fio.eof( ))
{
fio.read((char *)&emp1,sizeof (emp1));
emp1.putdata();
}
fio.close();
getch();
}[/code]
Many time working with file(adding record &then printing on screen or after deletion print the rec )last rec is read twice or otherwise each rec ie read twice I have checked my code.Still it's not working Pls help me If u can give correct code it will help me.
[Code=cpp]
#include<fstrea m.h>
#include<conio. h>
#include<stdio. h>
#include<string .h>
#include<proces s.h>
class emp
{
int id;
char name[20];
float sal;
public:
void getdata()
{
cout<<"Enter id:="<<"\n";
cin>>id;
cout<<"Enter name:="<<"\n";
cin>>name;
cout<<"Enter salary:="<<"\n" ;
cin>>sal;
}
void putdata()
{
cout<<"Id :="<<"\t "<<id<<endl ;
cout<<"Name :="<<"\t "<<name<<en dl;
cout<<"Salary :="<<"\t "<<sal<<end l;
}
int getid()
{
return id;
}
void modify();
}e1,emp1;
void emp::modify()
{
cout<<"Emp id" <<id<<"\t";
cout<<"Emp name" <<name<<"\t";
cout<<"Emp Salary" <<sal<<"\n";
cout<<"Enter new details "<<endl;
char nm[20]=" ";
int sal1;
cout<<"New Name :(enter '.' to retain old one)";
cin>>nm;
cout<<"New Salary :(enter '-1' to retain old one)";
cin>>sal1;
if(strcmp(nm,". ")!=0)
strcpy(name,nm) ;
if(sal1!=-1)
sal=sal1;
}
void main()
{
long pos;
char found='f';
int id1;
clrscr();
fstream fio;
fio.open("emp.d at",ios::app|io s::out|ios::bin ary);
emp1.getdata();
fio.write((char *)&emp1,sizeof (emp1));
cout<<"The contents before modification \n";
while(!fio.eof( ))
{
fio.read((char *)&emp1,sizeof (emp1));
emp1.putdata();
}
cout<<"Enter id of an Emploee whose record is to be modified "<<endl;
cin>>id1;
fio.seekg(0);
while(fio)
{
pos=fio.tellg() ;
fio.read((char *)&e1,sizeof (e1));
if(e1.getid()== id1)
{
e1.modify();
fio.seekg(pos);
fio.write((char *)&e1,sizeof(e1 ));
found='t';
break;
}
}
if(found=='f')
cout<<"Record not found !!\n";
fio.seekg(0);
cout<<"Now the contents \n";
while(!fio.eof( ))
{
fio.read((char *)&emp1,sizeof (emp1));
emp1.putdata();
}
fio.close();
getch();
}[/code]
Comment