ICode9

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

C++ 读raw和合并raw

2021-04-02 13:01:13  阅读:595  来源: 互联网

标签:pt int 合并 C++ raw fout include pixel


今天只做了两件事,学会C++读文件ifstream和写文件ofstream.记录下来,以后别忘记了。

用于读raw时的注意事项:

1.如果顺序读取,必须使用interleave的raw文件格式。即rgbrgbrgbrgb。

如果使用noninterleave,则变为:rrrrggggbbbb。读出来的数据会变。注意。

2.合并时,注意数据转换,如果是char转成int,就变成ASCII码了。所以尽量不要使用数据转换。

具体两个代码如下:

读取raw

文件为height65,width60.interleave的raw文件

#include<windows.h>
#include
#include
#include
using namespace std;

int main(void)
{
BYTE *pixel;
ifstream fin(“2.raw”,ios::binary);
if(!fin)
{
cerr<<“Open file error!”<<endl;
return false;
}
pixel=new BYTE[11700];
fin.read((char *)(pixel),11700);

int k = 0,i,j;
int r,g,b;
BYTE *pt;
ofstream fout;
int t=0;

fout.open(“2.txt”,ios::out);
for(i=0;i<65;i++)
{
for(j=0;j<60;j++)
{
pt =pixel+i*(603)+3j;
r = (int)(pt);
g = (int)(
(pt+1));
b = (int)(*(pt+2));
cout<<r<<" “<<g<<” “<<b<<” “;
fout<<r<<” “<<g<<” “<<b<<” ";
}
fout<<endl;
}
fout.close();
delete [] pixel;
pixel = NULL;

system(“pause”);
return 0;

}

2.合并

合并10个txt文件,到2.txt

#include
#include
#include
#include <stdLib.h>
#include
#include <math.h>
#include
#include
#include <time.h>
#include <windows.h>
using namespace std;

int main(void)
{
bool mergeRaw(char Filename[]);
BYTE *pixel;
BYTE *pt;
stringstream in_name;

for (int i=0;i<11;i++)
{
in_name.str("");
in_name<<“txt/”<< i+1000 <<".txt";

ifstream fin(in_name.str().c_str());
if(!fin)
{
cerr<<“Open file error!”<<endl;
return 0;
}
pixel=new BYTE [3];
fin.read((char *)(pixel),3);
ofstream fout(“txt/2.txt”,ios::app);
for(int i=0;i<3;i++)
{
pt=pixel+i;
cout<<(char)(*pt); //这里如果用(int)(*pt),结果将变成ASCII码。
fout<<(char)(*pt);
}
fout.close();

fin.close();
}
delete [] pixel;
pixel = NULL;
system(“pause”);
return 0;
}

标签:pt,int,合并,C++,raw,fout,include,pixel
来源: https://blog.csdn.net/syxzsyxz1/article/details/115395924

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

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

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

ICode9版权所有