0

这是一个简单的随机存取归档程序。问题出现在我想随机写入数据的地方。如果我在文件中写入任何位置,则以前的记录设置为 0。正在输入的最后一个 1 当前保存正确的值,所有其他 = 0。

这是代码

#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;

}
}

我无法理解 Y 以前的记录转为 0。

4

2 回答 2

2

编辑:在 MSDN 上找到文档。

ios::trunc - 如果文件已经存在,则丢弃其内容。如果指定了 ios::out 并且未指定 ios::ate、ios::app 和 ios:in,则隐含此模式

来自http://msdn.microsoft.com/en-us/library/aa277521%28v=vs.60%29.aspx,但由于官方标准没有改变,这应该是有效的。

于 2010-12-28T19:50:22.057 回答
0

包括

包括

包括

使用命名空间标准;

类名 { int id; 国际通行证;公共:无效的writeBlank();无效写入数据();无效读取数据();无效 readall(); int getid() { 返回 id; }

int getpass() { 返回通行证;}

无效 setid(int i) { id=i; }

无效 setpass(int p) { pass=p; } }; 无效名称::writeBlank() { 人名;person.setid(0); person.setpass(0);

诠释我;

ofstream out("pass.txt",ios::binary);

if ( !out ) { cout << "文件无法打开。" <<endl; }

for(i=0;i<10;i++) //制作 10 条记录 { cout<<"Put 指针在:"<(&person),sizeof(name));

} }

void name::writedata() { ofstream out("pass.txt",ios::binary|ios::ate); 名称 n1; int iD,p; cout<<"身份证号?"; cin>>iD; n1.setid(iD); cout<<"输入密码"; 辛>>p; n1.setpass(p);

if (!out) { cout << "文件无法打开。" <<endl; }

out.seekp((n1.getid()-1)*sizeof(name),ios::beg); //指针移动到我们必须根据其ID(索引)存储密码的所需位置 cout<<"文件指针位于:"<

out.write(reinterpret_cast (&n1), sizeof(name)); //写在那个指向的位置}

无效名称::readall() { 名称 n1;

ifstream in("pass.txt",ios::binary); if ( !in ) { cout << "文件无法打开。" <<endl; }

in.read(reinterpret_cast(&n1), sizeof(name));
while ( !in.eof() ) {
// 显示记录 cout<

  // read next from file                               
  in.read( reinterpret_cast< char * >(&n1), sizeof(name));                               

} // 结束而 }

无效名称::readdata() { ifstream in("pass.txt",ios::binary); if ( !in ) { cout << "文件无法打开。" <<endl; } in.seekg((getid()-1)sizeof(name)); //指针移动到我们必须根据其 ID(index) 读取密码的所需位置 cout<<"文件指针位于:"< )this,sizeof(name)); //从指向的位置读取 cout<

int main() {

名称 n1; cout<<"输入0写入空白记录"<>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;

} }

于 2010-12-28T21:59:41.813 回答