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

含む

含む

含む

名前空間 std を使用します。

クラス名 { int id; int パス; public: void writeBlank(); void writedata(); void readdata(); void readall(); int getid() { ID を返します。}

int getpass() { リターンパス; }

void setid(int i) {id=i; }

void setpass(int p) { pass=p; } }; void name::writeBlank() { 名前の人; person.setid(0); person.setpass(0);

int i;

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<<"ID?"; シン>>iD; n1.setid(ID); cout<<"パスワードを入力してください"; シン>> p; n1.setpass(p);

if (!out ) { cout << "ファイルを開けませんでした。" << endl; }

out.seekp((n1.getid()-1)*sizeof(name),ios::beg); //ポインターは、ID(index) に従ってパスワードを保存する必要がある目的の場所に移動します cout<<"ファイル ポインターは次の場所にあります: "<

out.write(reinterpret_cast (&n1), sizeof(name)); // 指定された場所に書き込む }

void 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));                               

} // 途中で終了 }

void name::readdata() { ifstream in("pass.txt",ios::binary); if ( !in ) { cout << "ファイルを開けませんでした。" << endl; in.seekg((getid()-1)sizeof(名前)); //ポインターは、ID(index) に従ってパスワードを読み取る必要がある目的の場所に移動します cout<<"ファイル ポインターは次の場所にあります: "< )this,sizeof(name)); //指定された場所から読み取る cout<

int main() {

名前 n1; cout<<"Enter 0 to write blank records"<>option; while(オプション==0 || オプション==1 || オプション==2 || オプション==3) { if (オプション== 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 に答える