Strange problem with Random Access Filing in C++

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.

Answers


EDIT: Found the documentation on MSDN.

ios::trunc - If the file already exists, its contents are discarded. This mode is implied if ios::out is specified and ios::ate, ios::app, and ios:in are not specified.

From http://msdn.microsoft.com/en-us/library/aa277521%28v=vs.60%29.aspx , but since the official standard hasn't changed, this should be valid.


include

include

include

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: "<(&person),sizeof(name));

} }

void name::writedata() { ofstream out("pass.txt",ios::binary|ios::ate); 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.write(reinterpret_cast (&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(&n1), sizeof(name) ); while ( !in.eof() ) { // display record cout<

  // 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: "< )this,sizeof(name)); //reads from that pointed location cout<

int main() {

name n1; cout<<"Enter 0 to write blank records"<>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;

} }


Need Your Help

Show/Make a field editable or disabled in admin based on other field's value

django django-models django-admin

I am working on a Django project and want to achieve the following functionality :-

JavaFX TableView: Cannot Override '-fx-text-fill'!

javafx-2 tableview

On JavaFX2.2, I created my own style class to represent table cells that contain invalid data. But my style class seems unable to override -fx-text-fill in the hover, selected, and focused states....

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.