C++ 数据转换总结

void RawImage::readStream(signed int * buf,char const *filename,int size){int lx=0,ly=0,lz=0;ifstream file;file.open(filename, ios::out | ios::app | ios::binary);if (!file.is_open()) {cout<< "The file open failed, Please check it and try again"<< endl;exit(0);}file.read(reinterpret_cast(&lx),sizeof(int));file.read(reinterpret_cast(&ly),sizeof(int));file.read(reinterpret_cast(&lz),sizeof(int));cout<<sizeof(int)<<endl;cout<<sizeof(short)<<endl;cout<<"lx="<<lx<<",ly="<<ly<<",lz="<<lz<<endl;file.seekg(24L,ios::beg);file.read((char *)buf,size);file.close();}以上是对raw文件的代码,如果不用reinterpret_cast就会出现运行时错误警告

void RawImage::readStream(signed int * buf,char const *filename,int size)
{
	int lx=0,ly=0,lz=0;
	ifstream file;
	file.open(filename, ios::out | ios::app | ios::binary);
	if (!file.is_open()) {
		cout<< "The file open failed, Please check it and try again"<< endl;
		exit(0);
	}
	file.read(reinterpret_cast<char *>(&lx),sizeof(int));
	file.read(reinterpret_cast<char *>(&ly),sizeof(int));
	file.read(reinterpret_cast<char *>(&lz),sizeof(int));
	cout<<sizeof(int)<<endl;
	cout<<sizeof(short)<<endl;
		cout<<"lx="<<lx<<",ly="<<ly<<",lz="<<lz<<endl;
	file.seekg(24L,ios::beg);
	file.read((char *)buf,size);
	file.close();


}



你可能感兴趣的:(C++ 数据转换总结)