//png和jpg图片也类似。输入输出都改为png格式图片。
//*********** 路径
string sPicPath = CCFileUtils::sharedFileUtils()->getWritablePath() + “voiceOn.png”;
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
string sMydata = CCFileUtils::sharedFileUtils()->getWritablePath() + “myData.txt”;
//***********
*
函数 ftell 用于得到文件位置指针
当前位置
相对于
文件首
的偏移字节数。
unsigned long pSize = 0;
unsigned char *pBuffer = NULL;
FILE *fp = fopen(sPicPath.c_str(),”rb”); //读取某张图片
if(!fp) return false;
fseek(fp, 0,
SEEK_END);
pSize =
ftell(fp); //获取图片大小
fseek(fp, 0,
SEEK_SET);
pBuffer =
new unsigned char[pSize]; //有new就要有delete,这边是delete[] pBuff; pBuff = NULL;
pSize = fread(pBuffer, sizeof(unsigned char), pSize, fp);
fclose(fp);
if(!pBuffer) return false;
//最后要删除
delete[] pBuffer;
pBuffer = 0;
方法一,基于C++的文件操作: (可进行定位读、定位写)
1、
将png图片以二进制文件的形式读入并保持在二进制文件中:
string sPicPath = CCFileUtils::sharedFileUtils()->getWritablePath() + “voiceOn.png”;
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
int nRead = 0;
char chBuf[1024] = {0};
//这边可以控制图片的大小,1024 = 1KB, 1024*1024 = 1M
string sData;
ifstream fin(sPicPath.c_str(), ios::binary);
if(!fin) return false;
fin.read(chBuf, sizeof(chBuf));
if(fin.eof() != 1)
//图片大小判断
{
CCLog(“pic is bigger than my array!”);
fin.close();
return false;
}
fin.close();
ofstream fout(sSavePath.c_str(), ios::binary);
if(!fout) return false;
fout.write(chBuf, sizeof(chBuf));
fout.close();
2、将二进制文件读出并保持至新的png图片:
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
string sGetPic = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.png”;
int nRead = 0;
char chBuf[1024] = {0};
ifstream fin(sSavePath.c_str(), ios::binary);
if(!fin) return;
fin.read(chBuf, sizeof(chBuf));
/* if(fin.eof() != 1)
{
CCLog(“pic is bigger than my array!”);
fin.close();
return;
}*/
fin.close();
ofstream fout(sGetPic.c_str(), ios::binary);
if(!fout) return;
fout.write(chBuf, sizeof(chBuf));
fout.close();
方法一 (a),基于C++的文件操作(定位在文件指定位置进行读写):
1、
将png图片以二进制文件的形式读入并保持在二进制文件中:
string sPicPath = CCFileUtils::sharedFileUtils()->getWritablePath() + “voiceOn.png”;
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
int nRead = 0;
char chBuf[1024] = {0};
ifstream fin(sPicPath.c_str(), ios::binary);
if(!fin) return false;
fin.read(chBuf, sizeof(chBuf));
if(fin.eof() != 1)
{
CCLog(“pic is bigger than my array!”);
fin.close();
return false;
}
fin.close();
ofstream fout(sSavePath.c_str(), ios::binary);
if(!fout) return false;
fout.
seekp(1024, ios::beg);
//定位到文件1024位置进行写操作
fout.
write(chBuf, sizeof(chBuf));
fout.close();
2、将二进制文件读出并保持至新的png图片:
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
string sGetPic = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.png”;
int nRead = 0;
char chBuf[1024] = {0};
ifstream fin(sSavePath.c_str(), ios::binary);
if(!fin) return;
fin.
seekg(1024, ios::beg);
//定位到文件1024位置进行读操作
fin.
read(chBuf, sizeof(chBuf));
/*if(fin.eof() != 1)
{
CCLog(“pic is bigger than my array!”);
fin.close();
return;
}*/
fin.close();
ofstream fout(sGetPic.c_str(), ios::binary);
if(!fout) return;
fout.write(chBuf, sizeof(chBuf));
fout.close();
方法二,用流式FILE文件操作: (可进行定位读、 定位写)
1、将png图片保存到二进制文件中;
string sPicPath = CCFileUtils::sharedFileUtils()->getWritablePath() + “test.png”;
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
int nRead = 0;
char chBuf[256] = {0};
FILE *fin = fopen(sPicPath.c_str(), “rb”);
FILE *fout = fopen(sSavePath.c_str(), “wb”);
if(fin == NULL || fout == NULL) return false;
nRead = fread(chBuf,sizeof(char), 256, fin);
while (true)
{
fwrite(chBuf, sizeof(char), 256, fout);
if(nRead < 256) break;
nRead = fread(chBuf, sizeof(char), 256, fin);
}
fclose(fin);
fclose(fout);
string sPicPath = CCFileUtils::sharedFileUtils()->getWritablePath() + “voiceOn.png”;
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
int nRead = 0;
unsigned long pSize = 0;
unsigned char *
pBuffer = NULL;
FILE *fp = fopen(sPicPath.c_str(),”rb”);
if(!fp) return false;
fseek(fp, 0, SEEK_END);
pSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
pBuffer =
new unsigned char[
pSize];
if(!pBuffer) return false;
pSize = fread(pBuffer, sizeof(unsigned char), pSize, fp);
fclose(fp);
FILE *fout = fopen(sSavePath.c_str(), “wb”);
if(!fout) return false;
unsigned long nWSize = 0;
fseek(fout, 1024, SEEK_SET);
nWSize = fwrite(pBuffer, sizeof(unsigned char), pSize, fout);
fclose(fout);
delete[] pBuffer;
pBuffer = 0;
2、将二进制文件读出图片;
string sSavePath = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.bat”;
string sGetPic = CCFileUtils::sharedFileUtils()->getWritablePath() + “mysavePic.png”;
int nRead = 0;
char chBuf[256] = {0};
FILE *fin = fopen(sSavePath.c_str(), “rb”);
FILE *fout = fopen(sGetPic.c_str(), “wb”);
if(fin == NULL || fout == NULL) return ;
nRead = fread(chBuf, sizeof(char), 256, fin);
while (1)
{
fwrite(chBuf,sizeof(char),256,fout);
if(nRead < 256) break;
nRead = fread(chBuf,sizeof(char), 256, fin);
}
fclose(fin);
fclose(fout);