帮忙看看这段代码错到哪了

tianfu1 2008-10-13 01:00:00
昨天研究了一晚上文件操作,今天写了段代码,编译时没错误,运行时却提示:某段内存不能读。我用的是vc++6.0

代码如下:
//main.cpp

#include<iostream>
#include<fstream>
#include"File1.h"

using namespace std;

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
Account a("111", "as", "123", 12.3), b("222", "sa", "321", 32.1);
fstream file;
file.open("c:\\a.txt", ios::in|ios::out|ios::binary|ios::app);
char* c;
file.write((char*)&a, sizeof(Account));
file.write((char*)&b, sizeof(Account));
if (!file.is_open())
{
cerr<<"can't open the file";
return 0;
}

while (!file.eof())
{
file.read(c, sizeof(Account));
cout<<c;
}

return 0;
}

//File1.cpp

#include<string>

using namespace std;

class Account
{
string name;
string id;
string password;
double money;

public:
Account(string n, string i, string p, double m=0)
{
name = n;
id = i;
password = p;
money = m;
}
};


各位帮忙看一看
...全文
98 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfu1 2008-10-13
  • 打赏
  • 举报
回复
虽然可以了,但输出的却是一大堆乱码,但毕竟这个问题解决啦
双子东宝 2008-10-13
  • 打赏
  • 举报
回复
路过,看见已经有回答了
jia_xiaoxin 2008-10-13
  • 打赏
  • 举报
回复
//我感觉楼主最好这样写:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Account
{
friend ostream & operator<< (ostream& os, const Account & person);
friend istream & operator>> (istream& in, Account & person);
string name;
string id;
string password;
double money;

public:
Account(){};
Account(string n, string i, string p, double m=0)
{
name = n;
id = i;
password = p;
money = m;
}
};

ostream& operator << (ostream& os, const Account & person)
{
os<<person.name<<endl;
os<<person.id<<endl;
os<<person.password<<endl;
os<<person.money<<endl;
return os; //返回...
}

istream& operator >> (istream& in, Account & person)
{
in >> person.name;
in >> person.id;
in >> person.password;
in >> person.money;
return in; //返回...
}

//---------------------------------------------------------------------------

int main(int argc, char* argv[])
{
Account a("111", "as", "123", 12.3), b("222", "sa", "321", 32.1);

fstream fout;
fout.open("c:\\a.txt", ios::out|ios::binary|ios::app);

//char * c;

//file.write((char*)&a, sizeof(Account));
//file.write((char*)&b, sizeof(Account));
if (!fout.is_open())
{
cerr <<"can't open the file";
return 0;
}
fout << a;
fout << b;
fout.close();

fstream fin;
fin.open("c:\\a.txt", ios::in|ios::binary);

while (!fin.eof())
{
Account c;
//file.read(c, sizeof(Account));
fin >> c;
//cout << c;
cout << c;
}
fin.close();

return 0;
}
devil_zuiai 2008-10-13
  • 打赏
  • 举报
回复
把自定义对象写入文件和读出文件
需要序列化和反序列化吧??
三文鱼也会飞 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chlaws 的回复:]
char* c;
没有分配内存 不能往里写数据
c=new char[10];
[/Quote]

正解
chlaws 2008-10-13
  • 打赏
  • 举报
回复
char* c;
没有分配内存 不能往里写数据
c=new char[10];

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧