C++ C+中随机存取归档的奇怪问题+;
这是一个简单的随机存取归档程序。问题出现在我想随机写入数据的地方。如果我在文件中写入任何位置,以前的记录将设置为0。当前输入的最后一个1包含正确的值all others=0 这是密码C++ C+中随机存取归档的奇怪问题+;,c++,file-handling,random-access,C++,File Handling,Random Access,这是一个简单的随机存取归档程序。问题出现在我想随机写入数据的地方。如果我在文件中写入任何位置,以前的记录将设置为0。当前输入的最后一个1包含正确的值all others=0 这是密码 #include <iostream> #include<fstream> #include<string> using namespace std; class name { int id; int pass; public: void writeBlank();
#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;
}
}
#包括
#包括
#包括
使用名称空间std;
类名
{
int-id;
国际通行证;
公众:
void writeBlank();
无效写入数据();
void readdata();
void readall();
int getid()
{
返回id;
}
int getpass()
{
回程通行证;
}
void setid(inti)
{
id=i;
}
无效设置传递(INTP)
{
通过=p;
}
};
void name::writeBlank()
{
姓名;
person.setid(0);
person.setpass(0);
int i;
流输出(“pass.txt”,ios::binary);
如果(!out)
{
coutEDIT:在MSDN上找到文档
ios::trunc-如果文件已存在,则其内容将被丢弃。如果指定了ios::out,而未指定ios::ate、ios::app和ios:in,则暗示此模式
从,但由于官方标准没有更改,因此该标准应有效。include
包括
包括
使用名称空间std
类名
{
int-id;
国际通行证;
公众:
void writeBlank();
无效写入数据();
void readdata();
void readall();
int getid()
{
返回id;
}
int getpass()
{
回程通行证;
}
void setid(inti)
{
id=i;
}
无效设置传递(INTP)
{
通过=p;
}
};
void name::writeBlank()
{
姓名;
person.setid(0);
person.setpass(0)
int i
流输出(“pass.txt”,ios::binary)
如果(!out)
{
CUT这个问题是关于C++而不是C的,所以我删除了C标记。使用FROW OUT(“PASS。TXT”,IO::二进制二进制::OUTIO:APP);@DumbCoder:ios::app将不起作用-这实际上会强制所有写入到文件的结尾。ios::ate应该可以起作用,尽管文档表明第一次写入将在文件的当前结尾结束。我想做的是,用户输入写入记录的位置,例如,文件中写入了3条记录。用户输入2总共应该有4条记录,当前记录写在第2位。这只能通过尝试ios::app不工作来完成,只在最后写入。不在其他位置。ios::trunc截断文件以前的数据丢失。我认为ios::ate是正确的选项…抱歉,我没有提到我应该用ios::ate代替ios::out但它仍然不起作用..首先尝试了1..从我在文档中读到的内容来看,ios::ate将在文件末尾打开后进行第一次写入,然后尊重您的tellp调用。许多程序处理编辑现有文件的一种方法是将其读入内存,更新,然后写出。通常甚至与原始文件一起保存的“暂存”文件(该暂存文件的存在可用于崩溃恢复)。编辑您的问题。不要为此发布新的答案。
// read next from file
in.read( reinterpret_cast< char * >(&n1), sizeof(name));
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;