Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C+中的CArchive类从二进制文件读取短数据+;_C++_File_Buffer_Short - Fatal编程技术网

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();