ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c/c++读写png照片

2022-03-20 11:03:27  阅读:232  来源: 互联网

标签:std string 读写 c++ cCache ilength iPicture png pw


seekg()对文件定位,有两个参数:偏移量和基地址。
偏移量:正负数值,正的表示向后偏移,负的表示向前偏移
基地址:表示输入流的位置,3种表示:
ios::beg:开始位置
ios::cur:当前位置
ios::end:结束位置
tellg()函数无参数,返回当前定位指针的位置,也代表着输入流的大小。

#include <fstream> // ifstream, ifstream::in
#include <iostream>
void writePicture(std::string path, char* cContent, int ilength)
{
	FILE* pw;
	pw = std::fopen(path.c_str(), "wb");
	fwrite(cContent, sizeof(char), ilength, pw);
	fflush(pw);
	fclose(pw);
}
void ReadWritePicture(const std::string& szReadPath, const std::string& szWritePath)
{
	// 1. 打开图片
	std::ifstream iPicture(szReadPath, std::ifstream::in | std::ios::binary);
	// 2. 计算图片长度
	iPicture.seekg(0, iPicture.end);    //基地址为结束位置,偏移量为0
	int length = iPicture.tellg();
	iPicture.seekg(0, iPicture.beg);     //基地址为结束位置,偏移量为0
	// 3. 创建内存缓存区
	char* cCache = new char[length];
	// 4. 读取图片到内存中
	iPicture.read(cCache, length);
	iPicture.close();
	writePicture(szWritePath, cCache, length);
	if (nullptr != cCache)
	{
		delete[] cCache;
	}
}
int main() {
	std::string szReadPath = "test.png";
	std::string szWritePath = "image.png";
	ReadWritePicture(szReadPath, szWritePath);
	return 0;
}
#include <fstream> // ifstream, ifstream::in
#include <iostream>

void ReadPicture(const std::string& szReadPath, std::string& szContent, int& ilength)
{
	// 1. 打开图片
	std::ifstream iPicture(szReadPath, std::ifstream::in | std::ios::binary);
	// 2. 计算图片长度
	iPicture.seekg(0, iPicture.end);    //基地址为结束位置,偏移量为0
	ilength = iPicture.tellg();
	iPicture.seekg(0, iPicture.beg);     //基地址为结束位置,偏移量为0
	// 3. 创建内存缓存区
	char* cCache = new char[ilength];
	// 4. 读取图片到内存中
	iPicture.read(cCache, ilength);
	iPicture.close();

	szContent.assign(reinterpret_cast<char*>(&cCache), sizeof(cCache));
	/*if (nullptr != cCache)
	{
		delete[] cCache;
	}*/
}
void writePicture(const std::string& szWritePath, const std::string& szContent, const int& ilength)
{
	FILE* pw;
	pw = std::fopen(szWritePath.c_str(), "wb");
	char* cCache = new char[ilength];
	memcpy(&cCache, szContent.c_str(), sizeof(cCache));
	fwrite(cCache, sizeof(char), ilength, pw);
	fflush(pw);
	fclose(pw);
	if (nullptr != cCache)
	{
		delete[] cCache;
	}
}

int main() {
	std::string szReadPath = "test.png";
	std::string szWritePath = "image.png";
	std::string szContent = "";
	int ilength = 0;
	ReadPicture(szReadPath, szContent, ilength);
	writePicture(szWritePath, szContent, ilength);
	return 0;
}

标签:std,string,读写,c++,cCache,ilength,iPicture,png,pw
来源: https://blog.csdn.net/qq_34552942/article/details/123608150

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有