This is a simple random access filing program . The problem arises where i want to write data randomly. If I write any where in the file the previous records are set to 0. the last 1 which is being entered currently holds the correct value all others =0.
This is the code
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
class name
{
int id;
int pass;
public:
void writeBlank();
void writedata();
void readdata();
void readall();
int getid()
{
return id;
}
int getpass()
{
return pass;
}
void setid(int i)
{
id=i;
}
void setpass(int p)
{
pass=p;
}
};
void name::writeBlank()
{
name person;
person.setid(0);
person.setpass(0);
int i;
ofstream out("pass.txt",ios::binary);
if ( !out )
{
cout << "File could not be opened." << endl;
}
for(i=0;i<10;i++) //make 10 records
{
cout<<"Put pointer is at: "<<out.tellp()<<endl;
cout<<"Blank record "<<i<<" is: "<<person.getid()<<" "<<person.getpass()<<" and size: "<<sizeof(person)<<endl;
cout<<"Put pointer is at: "<<out.tellp()<<endl;
out.write(reinterpret_cast< const char * >(&person),sizeof(name));
}
}
void name::writedata()
{
ofstream out("pass.txt",ios::binary|ios::out);
name n1;
int iD,p;
cout<<"ID?";
cin>>iD;
n1.setid(iD);
cout<<"Enter password";
cin>>p;
n1.setpass(p);
if (!out )
{
cout << "File could not be opened." << endl;
}
out.seekp((n1.getid()-1)*sizeof(name),ios::beg); //pointer moves to desired location where we have to store password according to its ID(index)
cout<<"File pointer is at: "<<out.tellp()<<endl;
out.write(reinterpret_cast<const char*> (&n1), sizeof(name)); //write on that pointed location
}
void name::readall()
{
name n1;
ifstream in("pass.txt",ios::binary);
if ( !in )
{
cout << "File could not be opened." << endl;
}
in.read( reinterpret_cast<char *>(&n1), sizeof(name) );
while ( !in.eof() )
{
// display record
cout<<endl<<"password at this index is:"<<n1.getpass()<<endl;
cout<<"File pointer is at: "<<in.tellg()<<endl;
// read next from file
in.read( reinterpret_cast< char * >(&n1), sizeof(name));
} // end while
}
void name::readdata()
{
ifstream in("pass.txt",ios::binary);
if ( !in )
{
cout << "File could not be opened." << endl;
}
in.seekg((getid()-1)*sizeof(name)); //pointer moves to desired location where we have to read password according to its ID(index)
cout<<"File pointer is at: "<<in.tellg()<<endl;
in.read((char* )this,sizeof(name)); //reads from that pointed location
cout<<endl<<"password at this index is:"<<getpass()<<endl;
}
int main()
{
name n1;
cout<<"Enter 0 to write blank records"<<endl;
cout<<"Enter 1 for new account"<<endl;
cout<<"Enter 2 to login"<<endl;
cout<<"Enter 3 to read all"<<endl;
cout<<"Enter 9 to exit"<<endl;
int option;
cin>>option;
while(option==0 || option==1 || option==2 || option==3)
{
if (option == 0)
n1.writeBlank();
if(option==1)
{
/*int iD,p;
cout<<"ID?";
cin>>iD;
n1.setid(iD);
cout<<"Enter password";
cin>>p;
n1.setpass(p);*/
n1.writedata();
}
int ind;
if(option==2)
{
cout<<"Index?";
cin>>ind;
n1.setid(ind);
n1.readdata();
}
if(option == 3)
n1.readall();
cout<<"Enter 0 to write blank records"<<endl;
cout<<"Enter 1 for new account"<<endl;
cout<<"Enter 2 to login"<<endl;
cout<<"Enter 3 to read all"<<endl;
cout<<"Enter 9 to exit"<<endl;
cin>>option;
}
}
I Cant understand Y the previous records turn 0.