返回信息流简单的说,就是我要向一个文件中读写我自己定义的对象数据,请问,当我想把它从文件中读取时是先将对象读出到内存后,再根据定义的数据结构直接读那段数据的每个属性部分吗?还是先将其全部转化为字符串,再根据定义的数据结构来强行读取?
而且读到内存后,比如我读了一段数据到内存中,我知道它的数据结构,可是我如何将其属性读出来? 比如一个对象包含了string类型的一个属性,我如何将之读出并初始化?
这是一条镜像帖。来源:北邮人论坛 / cpp / #16341同步于 2008/11/19
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
请教达人关于C++里对于文件的大对象读写(代码)
lblz
2008/11/19镜像同步9 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
【 在 lblz (砺剑) 的大作中提到: 】
: 简单的说,就是我要向一个文件中读写我自己定义的对象数据,请问,当我想把它从文件中读取时是先将对象读出到内存后,再根据定义的数据结构直接读那段数据的每个属性部分吗?还是先将其全部转化为字符串,再根据定义的数据结构来强行读取?
前者
: 而且读到内存后,比如我读了一段数据到内存中,我知道它的数据结构,可是我如何将其属性读出来? 比如一个对象包含了string类型的一个属性,我如何将之读出并初始化?
就像普通初始化一样,str.attr=value,然后再将修改后的内容写入
【 在 Xer 的大作中提到: 】
: 前者
: 就像普通初始化一样,str.attr=value,然后再将修改后的内容写入
在类属性为简单类型的情况下我试过这样是没问题的,可是比如有个string的属性时,我强行读出到内存后初始化再打印出来时,程序运行就中断了,代码如下:
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
class Table
{
public:
string tab_name;
Table() {}
//Table(const string& test)
Table(string te)
{
tab_name=te;
}
};
class DataBase
{
public:
string db_nm;
DataBase(string db_name)
{
db_nm=db_name;
fstream file(db_nm.c_str(),ios_base::binary|ios::out);
if(file.fail()) { cout<<"Cannot open file!"<<endl; }
Table test;
//ifstream fin(db_nm.c_str(),ios_base::binary);
file.read((char*)&test,sizeof(Table)); //初始化,这里程序运行时就中断了,不知道是不是string的初始化问题?
file.close();
cout<<"OK here!";
cout<<test.tab_name<<endl;
}
~DataBase(){}
};
int main()
{
fstream file_test("D:\\For_test.db",ios_base::binary|ios::out|ios::in); //初始化
if(file_test.fail()) { cout<<"Cannot open file!"<<endl; }
//string str("student");
Table ta("student");
file_test.write((char*)&ta,sizeof(Table));
Table tb;
file_test.read((char*)&tb,sizeof(Table));
cout<<tb.tab_name<<endl;
file_test.close();
//DataBase("D:\\For_test.db");
return 1;
}
【 在 lblz (砺剑) 的大作中提到: 】
: 标 题: Re: 请教达人关于C++里对于文件的大对象读写
: 发信站: 北邮人论坛 (Wed Nov 19 21:26:31 2008), 站内
:
:
: 【 在 Xer 的大作中提到: 】
: : 前者
: : 就像普通初始化一样,str.attr=value,然后再将修改后的内容写入
:
: 在类属性为简单类型的情况下我试过这样是没问题的,可是比如有个string的属性时,我强行读出到内存后初始化再打印出来时,程序运行就中断了,代码如下:
: #include<string>
: #include<iostream>
: #include<fstream>
: using namespace std;
:
: class Table
: {
: public:
: string tab_name;
: Table() {}
: //Table(const string& test)
: Table(string te)
: {
: tab_name=te;
: }
: };
:
: class DataBase
: {
: public:
: string db_nm;
: DataBase(string db_name)
: {
: db_nm=db_name;
: fstream file(db_nm.c_str(),ios_base::binary|ios::out);
~~~~~~~~~~读数据的话这里应该是ios::in吧?
: if(file.fail()) { cout<<"Cannot open file!"<<endl; }
: Table test;
: //ifstream fin(db_nm.c_str(),ios_base::binary);
: file.read((char*)&test,sizeof(Table)); //初始化,这里程序运行时就中断了,不知道是不是string的初始化问题?
: file.close();
: cout<<"OK here!";
: cout<<test.tab_name<<endl;
: }
:
: ~DataBase(){}
: };
:
: int main()
: {
: fstream file_test("D:\\For_test.db",ios_base::binary|ios::out|ios::in); //初始化
: if(file_test.fail()) { cout<<"Cannot open file!"<<endl; }
: //string str("student");
: Table ta("student");
: file_test.write((char*)&ta,sizeof(Table));
: Table tb;
: file_test.read((char*)&tb,sizeof(Table));
: cout<<tb.tab_name<<endl;
: file_test.close();
: //DataBase("D:\\For_test.db");
: return 1;
: }
:
: --
:
: ※ 修改:·lblz 于 Nov 19 21:39:18 修改本文·[FROM: 211.86.153.*]
: ※ 来源:·北邮人论坛 http://forum.byr.edu.cn·[FROM: 211.86.153.*]
【 在 Xer 的大作中提到: 】
: ~~~~~~~~~~读数据的话这里应该是ios::in吧?
额... 这个地方我改了下,改过后程序运行时还是会中断,如下:
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
class Table
{
public:
string tab_name;
Table() {}
//Table(const string& test)
Table(string te)
{
tab_name=te;
}
};
class DataBase
{
public:
string db_nm;
DataBase(string db_name)
{
db_nm=db_name;
fstream file(db_nm.c_str(),ios_base::binary|ios::out|ios::in);
if(file.fail()) { cout<<"Cannot open file!"<<endl; }
Table test;
//ifstream fin(db_nm.c_str(),ios_base::binary);
file.read((char*)&test,sizeof(Table)); //初始化
file.close();
cout<<"OK here!";
cout<<test.tab_name<<endl;
}
~DataBase(){}
};
int main()
{
fstream file_test("D:\\For_test.db",ios_base::binary|ios::out|ios::in); //初始化
if(file_test.fail()) { cout<<"Cannot open file!"<<endl; }
//string str("student");
Table ta("student");
file_test.write((char*)&ta,sizeof(Table));
Table tb;
file_test.read((char*)&tb,sizeof(Table));
cout<<(string)tb.tab_name<<endl; //每次运行到这一行就会出现中断,不知道怎么回事
file_test.close();
//DataBase("D:\\For_test.db");
return 1;
}
我觉得有问题吧
file_test.write((char*)&ta,sizeof(Table));
你把ta的内容写到文件里
写的其实是string Table::tab_name;
string里面是个char*指针吧
你把这个指针写到文件里
以后再从文件里读出来
指针所指的那段内存能保证还有效么
【 在 guo 的大作中提到: 】
: 我觉得有问题吧
: file_test.write((char*)&ta,sizeof(Table));
: 你把ta的内容写到文件里
: ...................
那这么说的话,对于字符串类型的对象的文件读写,只能用char*了?
我的意思是
涉及到文件操作的时候
是不是应该写指针所指向的内容,而不是指针?
按说应该这样吧
【 在 lblz 的大作中提到: 】
: 那这么说的话,对于字符串类型的对象的文件读写,只能用char*了?
【 在 guo 的大作中提到: 】
: 我的意思是
: 涉及到文件操作的时候
: 是不是应该写指针所指向的内容,而不是指针?
: ...................
我的意思是说比如一个对象内部包含字符串类型的属性,如果想用string从文件中读写这个
属性是不行的(因为读出来的那段数据里面是先前内存中的地址,而实际需要的是数据),必须用char*(就是字符串数组)来解决?