读写文件
Posted feng..liu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读写文件相关的知识,希望对你有一定的参考价值。
C++读写文件
#include <opencv/cv.h>
#include <fstream>
#include <iostream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
#define W 1280
#define H 720
#define size W*H*2
int main()
{
unsigned char y1, y2, u, v;
char *buffer = new char[size];
string inpath = "C:\\Users\\liufeng16\\Desktop\\测试\\UVUY\\yuv3_5";
string outpath = "C:\\Users\\liufeng16\\Desktop\\测试\\UVUY\\yuv0_5_outliufeng";
ifstream filein(inpath);
ofstream fileout(outpath);//结果
filein.read(buffer, size);
fileout.write(buffer, size);
for (int m = 0; m < W*H; m += 2)
{
u = buffer[m * 2];
y1 = buffer[m * 2 + 1];
v = buffer[m * 2 + 2];
y2 = buffer[m * 2 + 3];
fileout << y1;
fileout << u;
fileout << y2;
fileout << v;
}
filein.close();
fileout.close();
return 0;
}
c读写文件
#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>//不要忘记包含此头文件
#include <stdio.h>
using namespace std;
int main()
{
char *InputPath = "C:\\Users\\liufeng16\\Desktop\\测试\\UVUY\\yuv3_5";
char *OutputPath = "C:\\Users\\liufeng16\\Desktop\\测试\\UVUY\\yuv3_5.yuv";
char *InputBuf = new char[1280*720*2];
char *OutputBuf = new char[1280*720*2];
FILE *fpin,*fpout;
if( ( fpin = fopen(InputPath, "rb")) == NULL)
{
exit(0);
}
if ((fpout = fopen(OutputPath, "w+")) == NULL)
{
exit(0);
}
fread(InputBuf, 1, 1280 * 720 * 2, fpin);
strcpy(OutputBuf, InputBuf);
fwrite(InputBuf,1, 1280 * 720 * 2,fpout);
//cout << InputBuf;
fclose(fpin);
fclose(fpout);
return 0;
}
以上是关于读写文件的主要内容,如果未能解决你的问题,请参考以下文章