以二进制打开并读取文件,以十进制输出

来源:百度知道 编辑:UC知道 时间:2024/05/31 14:46:43
请问大虾们,在C++中当以二进制打开一个文件并读取4个字节,怎么把这4个字节分别以长整形和浮点形数输出到屏幕?急急急!!先谢过了!!!用fstream类.

#include <iostream>
#include <fstream>
using namespace std;
void main()
{
fstream file;
file.open("e:\\file.txt"); //路径随便改
char text[4];
if(!file)
{
cout<<"can not find that file!";
return;
}
file.read(text,4); //读取4个字节

cout<<reinterpret_cast<float &> (text)<<endl; //强制类型转换,下面类似
cout<<reinterpret_cast<long &> (text);
}