c++二进制文件

来源:百度知道 编辑:UC知道 时间:2024/05/15 23:41:39
建立某单位职工通信录二进制文件,文件中的每个记录包括职工编号,姓名,电话号码,邮政编码和住址。
我问一下,二进制文件怎么输出啊?
我写了个程序
#include <iostream.h>
#include <fstream.h>
#include <string.h>
main()
{char b,a;
fstream file("D:\\5.txt",ios::out|ios::in|ios::binary);
cin>>&b;
file.write(&b,sizeof(char));
long posend=file.tellg();
file.seekp(0,ios::beg);
file.seekg(0,ios::beg);
file.read(&a,sizeof(char));
cout<<&a+1<<',';
cout<<posend<<file.tellg()<<endl;
file.close();
}

文件流重载了操作符号<<和>>的,
你直接file<<或者file>>就可以了
你输出到屏幕的话,读一个字符输出1个:

char buf = '\0';
//每次读取一个字符
while (!file.eof())
{
file>>buf;
std::cout<<(buf);
}