1、读二进制文件的数据内容时,使用到 ifstream 类,三个步骤(1)打开文件 open 函数;(2)读文件 read 函数;(3)关闭文件 close 函数。
2、写二进制文件的数据内容时,使用到ofstream 类,三个步骤(1)打开文件 open 函数;(2)写文件 write 函数;(3)关闭文件 close 函数。
具体实现如下代码
// input
Mat img(720, 1280, CV_8UC1);
ifstream file;
file.open(_path, ios::binary); //> _path means path of files
if (file.is_open())
{
int length = 720 * 1280;
file.read((char*)img.data, length); //> input data from _path to img
}
file.close();
// output
ofstream file;
file.open(_path, ios::binary); //> _path means path of files
if (file.is_open())
{
int length = 720 * 1280;
file.write((const char*)(_img.data), length); //> out put from _img to _path
}
file.close();