Visual c++ 将二进制数写入bmp图像
我读取一个bmp图像,然后存储在字符串向量中 将字符转换为数字和二进制数:Visual c++ 将二进制数写入bmp图像,visual-c++,Visual C++,我读取一个bmp图像,然后存储在字符串向量中 将字符转换为数字和二进制数: typedef unsigned char BYTE; std::streampos fileSize; std::vector<BYTE> readFile(const char* filename) { // open the file: std::ifstream file(filename, std::ios::binary); // get its size: f
typedef unsigned char BYTE;
std::streampos fileSize;
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<BYTE> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
typedef无符号字符字节;
std::streampos文件大小;
std::矢量读取文件(常量字符*文件名)
{
//打开文件:
std::ifstream文件(文件名,std::ios::binary);
//获取其大小:
seekg(0,std::ios::end);
fileSize=file.tellg();
seekg(0,std::ios::beg);
//读取数据:
std::矢量文件数据(文件大小);
read((char*)&fileData[0],fileSize);
返回文件数据;
}
现在这很好,但我想在转换后重写bmp文件
将每个二进制数转换为一个字符,并将其存储在新文件中
ofstream saveFile(path);
int i=0; string str="";
while(i<binary.size()) //the binary_size is a string that contain all binary number of bmp
{
str=BinartToInt(binary[i]);//BinartToInt is a function that convert 8bit binary to number
saveFile <<str;
i++;
}
saveFile.close();
流存储文件(路径)的;
int i=0;字符串str=“”;
而(i确保输出流也被标记为二进制,否则行尾和其他文本处理将对二进制流生效
ofstream saveFile(path, std::ios::binary);
不能使用格式化输出函数将数据写入二进制格式文件。请使用saveFile.write()