第1个回答 2013-02-24
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct temp
{
char name[10];
int n;
};
void write(const temp& a)
{
ofstream fout("1.dat",ios::binary);
fout.write(reinterpret_cast<const char*>(&(a.name)),10);
fout.write(reinterpret_cast<const char*> (&(a.n)),sizeof(int));
}
void read(temp& a)
{
ifstream fin("1.dat");
fin.read(reinterpret_cast<char*>(&(a.name)),10);
fin.read(reinterpret_cast<char*>(&(a.n)),sizeof(int));
}
int main()
{
temp a={"zhangsan",14},b;
write(a);
read(b);
cout<<b.name<<b.n<<endl;
}
zhangsan14
请按任意键继续. . .
第2个回答 2013-03-07
你更苛刻的要求,或无法实现!我给你几个解决方案!
1:与特定的字符,如逗号分隔的文本文件,每个项目,空间,如
2:或二进制保存,但仍与一个具体的数字分离一般选举使用的号码,如为0xFF(255)
当然,这两个程序会遇到一个问题,你要保存所使用的数据分割为了逃避,在源代码中的字符或数据类似于\ \ \相同,但如果选择了正确的分离字符不能逃脱
3:还有一个程序长,如你在这里的结构,你可以保存10 - 组的名称,诠释节省4个字节,温度= {“张三”,14};保存7a68616e6773616e00000000000e(我写的十六进制数是张三\ 0 \ 00x0000000e,我相信你能理解),所以每次读14个字节10字节的名称,字符串,4个字节是n,int类型!
你认为自己是根据该程序的源代码,我不给你写信!