后来,请教老师去了,老师给了我一个回复 rfile.read((char*)users.begin(), users.size() );
之后,我上机,改了一下,一编译,错误,那是个迭代器啊?
于是,我查了一下read的语法,
read 语法:
istream &read( char *buffer, streamsize num );
函数read()用于输入流,在将字符放入buffer 之前从流中读取num 个字节。如果碰到EOF,read()中止,丢弃不论多少个字节已经放入。例如:
struct {
int height;
int width;
} rectangle;
input_file.read( (char *)(&rectangle), sizeof(rectangle) );
if( input_file.bad() ) {
cerr << "Error reading data" << endl;
exit( 0 );
}
write 语法:
ostream &write( const char *buffer, streamsize num );
write()函数用于输出流,从buffer中写num个字节到当前输出流中。
这次,我终于想明白了很多了,原来那个迭代的思路是有点对的,但是,在这里实现不了,在messageuser类中,
没有构造map,它是一个空容器,哪里会有地址可以让它放入呢?再说,map.begin()是不能放入数据的,得通过map.insert()函数,
所以,这就形成一个bug了,想了几个方法,创建新容器。。。失败,最后,还是用输入输出流解决问题了,将savehelp的函数改了一下,
将loadhelp的函数改了一下,最后,再将load()函数改了一下,这样一下,程序就完成了,剩下要做的就是给自己的程序写写说明了。
上来CSDN,最后还是自己给自己回答了,晕啊~~
/**
* bool messageuser::savedhelp();保存文件为二进制文件,若保存文件失败,则返回false;
* void messageuser::save(); 按要求输入保存文件的名称,完成保存文件功能。
*/
void messageuser::savehelp(string fname)
{
std::ofstream sfile;
sfile.open(fname.c_str(), std::ofstream::out | std::ofstream::binary);
type_map_si::iterator iter = users.begin();
while(iter != users.end() )
{
sfile << iter -> first << "\t\t" << iter -> second << " ";
++iter;
}
sfile.close();
return;
}
void messageuser::save()
{
using std::fstream;
using std::ofstream;
using std::ifstream;
using std::cout;
using std::cin;
fstream inout(savefilename.c_str(), fstream::out | fstream::in);
inout.close();
string sname("");
string comparen("");
char choice('r');
bool circulateInput;
bool circulateInput2;
cout << "请输入需要保存的文件名:";
do
{
inout.open(savefilename.c_str(), fstream::out | fstream::in);
circulateInput = false;
cin >> sname;
sname.append(".dat");
while( inout >> comparen )
// 文件名核对,是否有重名现象
if (comparen == sname)
{
cout << "\n注意,检测到已存在同名文件名。";
cout << std::unitbuf << "\n覆盖请按 " << "y"
<< " 否则请按 \"r\" 重新键入文件名:" << std::nounitbuf;
do
{
circulateInput2 = false;
cin >> choice;
while(!cin)
{
cin.clear();
cin.sync();
std::cerr << "\n输入错误,请重新输入选择:";
cin >> choice;
}
switch(choice)
{
case 'r': circulateInput = true;break;
case 'y': break;
default:
{
std::cerr << "\n输入错误,请重新输入选择:";
cin >> choice;
circulateInput2 = true;
break;
}
}
}while(circulateInput2);
}
inout.clear();
inout.close();
}while(circulateInput);
if (choice == 'r')
{
cout << "接受测试";
string cr(" ");
inout.open(savefilename.c_str(), fstream::out | fstream::in | fstream::app);
inout << sname << cr ;
inout.close();
}
savehelp(sname);
cout << "\n恭喜,文件保存成功!";
cout << std::endl;
return;
}
/**
* bool messageuser::loadhelp();检测是否已存在已保存的文件,若存在,则返回true;
* void messageuser::load(); 完成文件读取功能。
*/
void messageuser::loadhelp(string lfile)
{
std::ifstream rfile;
string temp;
int itemp;
rfile.open(lfile.c_str(), std::ifstream::in | std::ifstream::binary);
type_map_si::iterator iter = users.begin();
while(!rfile.eof())
{
rfile >> temp >> itemp;
users[temp] = itemp;
}
rfile.close();
return;
}
void messageuser::load()
{
using std::cout;
using std::cin;
using std::ifstream;
int i(0);
string ofile;
cout << "\n\n正在读取已存储的通讯录文件列表...\n";
std::set<string> TempSaveName;
ifstream reado;
reado.open(savefilename.c_str(), ifstream::in);
if (reado.good())
{
while(!reado.eof())
{
reado >> ofile;
TempSaveName.insert(ofile);
if(!reado)
; // if控制,防止多读取一次最后的空格,但是读取不到任何字符.
// 在文件中的格式为abc cba ,每个文件后都会有一个空格
else
{
cout << "\n" << ++i << ". "<< ofile;
}
}
cout << "\n\n请输入需要读取的文件名称,若不需读取,读输入\"no\":";
cin >> ofile;
if (TempSaveName.count(ofile) )
{
loadhelp(ofile);
cout << "\n读取成功!\n";
}
else if (ofile == "no")
{
cout << "\n操作成功!\n";
}
else
{
cout << "\n读取失败,请检查需要读取的文件的名字是否输入正确!"
<< "\n 还有一次输入机会,若输入错误,将直接进入系统:";
cin >> ofile;
if (TempSaveName.count(ofile) )
{
loadhelp(ofile);
cout << "\n读取成功!\n";
}
else if (ofile == "no")
{
cout << "\n操作成功!\n";
}
else
{
cout << "\n将直接进入系统操作。。。\n";
}
}
}
else
{
reado.clear();
cout << "\n尚未发现已存储的通讯文件。\n";
}
reado.close();
}