C++ 使用C+中的CArchive类从二进制文件读取短数据+;
我创建了一个应用程序,它将C++ 使用C+中的CArchive类从二进制文件读取短数据+;,c++,file,buffer,short,C++,File,Buffer,Short,我创建了一个应用程序,它将short数组存储到一个文件中。使用CArchive类 保存数据的代码 CFile objFile(cstr, CFile::modeCreate | CFile::modeWrite); CArchive obj(&objFile, CArchive::store); obj << Number; //int obj << reso; //int obj << height; //int obj <<
short
数组存储到一个文件中。使用CArchive类
保存数据的代码
CFile objFile(cstr, CFile::modeCreate | CFile::modeWrite);
CArchive obj(&objFile, CArchive::store);
obj << Number; //int
obj << reso; //int
obj << height; //int
obj << width; //int
int total = height * width;
for (int i = 0; i < total; i++)
obj << buffer[i];//Short Array
但是上面的代码并没有提供与我保存的数据相同的数据。那么,有人能告诉我如何使用
CArchive
或任何其他函数在short
数组中获取数据。假设缓冲区是一个short数组,加载数据的代码可以写为:
CFile objFile(cstr, CFile::modeRead);
CArchive obj(&objFile, CArchive::load);
obj >> Number; //int
obj >> reso; //int
obj >> height; //int
obj >> width; //int
int total = height * width;
//release the old buffer if needed... e.g:
if( buffer )
delete[] buffer;
//allocate the new buffer
buffer = new SHORT [total];
for (int i = 0; i < total; i++) {
obj >> buffer[i];
}
obj.Close();
objFile.Close();
cfileobjfile(cstr,CFile::modeRead);
CArchive-obj(&objFile,CArchive::load);
obj>>编号//int
obj>>reso//int
obj>>高度//int
obj>>宽度//int
int total=高度*宽度;
//如果需要,释放旧的缓冲区。。。例如:
if(缓冲区)
删除[]缓冲区;
//分配新的缓冲区
缓冲区=新短[总];
对于(int i=0;i>缓冲区[i];
}
obj.Close();
objFile.Close();
CFile objFile(cstr, CFile::modeRead);
CArchive obj(&objFile, CArchive::load);
obj >> Number; //int
obj >> reso; //int
obj >> height; //int
obj >> width; //int
int total = height * width;
//release the old buffer if needed... e.g:
if( buffer )
delete[] buffer;
//allocate the new buffer
buffer = new SHORT [total];
for (int i = 0; i < total; i++) {
obj >> buffer[i];
}
obj.Close();
objFile.Close();