[英]Strange problem with Random Access Filing in C++
這是一個簡單的隨機存取文件程序。 問題出現在我想隨機寫數據的地方。 如果我在文件中的任何位置寫入任何內容,則先前的記錄將設置為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。
編輯:在MSDN上找到文檔。
ios :: trunc - 如果文件已存在,則丟棄其內容。 如果指定了ios :: out並且未指定ios :: ate,ios :: app和ios:in,則隱含此模式 。
來自http://msdn.microsoft.com/en-us/library/aa277521%28v=vs.60%29.aspx ,但由於官方標准沒有改變,這應該是有效的。
using namespace std;
class name {int id; int pass; 公共: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; }}; 無效name :: writeBlank(){name person; 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 <<“把指針放在:”<(&person),sizeof(name));
}}
void name :: writedata(){ofstream out(“pass.txt”,ios :: binary | ios :: ate); 名字n1; int iD,p; COUT << “ID?”; CIN >>標識; n1.setid(ID); cout <<“輸入密碼”; CIN >> P; n1.setpass(P);
如果(!out){cout <<“無法打開文件。” << endl; }
out.seekp((n1.getid() - 1)*的sizeof(名稱),IOS :: BEG); //指針移動到我們必須根據其ID(索引)存儲密碼的所需位置cout <<“文件指針位於:”<
out.write(reinterpret_cast(&n1),sizeof(name)); //寫在那個尖頭的位置}
void name :: readall(){name 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(name)); //指針移動到我們必須根據其ID(索引)讀取密碼所需的位置cout <<“文件指針位於:”<)this,sizeof(name)); //從該指向的位置讀取cout <
int main(){
名字n1; cout <<“輸入0以寫入空白記錄” <>選項; 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;
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.