成都市龙祥路小学:C++如何在实现文件查寻?

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/30 10:39:20
我现在学会使用fstream.h头文件在磁盘建立文件,写文件和多文件了。可是怎么随机读写文件呢?
具体而言,我每次往文件中写一个人的信息,并且给他们编号,可我怎么来一输入编号,这个人的信息就全部出来了呢?

不懂C++流就不要乱评价.
C++流优性很高.
就举你上面的l例子:

ofstream in("Person.bin");
strstream str;
//写入
class people
{
private:
char name[20];
int Id;
char memo[255];
public:
people(int id);{Id=id}.
people(...);//其他构造函数
int GetId(){return Id;};
SetMemo();
char *GetMemo(char *me);
SetName(char *name);
char *GetName();
...

};

person *one=new person(1);// the first people's ID is 1

str<<one->GetId()<<one->GetMemo();
in<<str;

...
读:
ifstream out("Person.bin");
int tmp;
int yourId;
cin>>yourId;
char *buf=new char[sizeof(person)];

ostrstream str2(buf);
while(out.getline(tmp))
{
out.get();
if(tmp==yourId)
str2<<out.rdout();//????
}

cout<<str2<<endl;

用标准C库的fopen,fseek,fread。把这个用熟就成了。基本上够用一辈子。

我从来不用C++的stream,

一个词来评价,就是没用。

除非你想设计一些流一样风格的类,否则,这个标准输入出流,没有用。