读写文件

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;

    
}

void YUYV_To_RGB(char * inputpath, char * outputpath)
{
long Start, End, Size;
unsigned char y1, y2, u, v;
unsigned char r1, g1, b1;
unsigned char r2, g2, b2;
unsigned char *buffer = new unsigned char[Hight*Width * 2];
Mat rgbimg(Hight, Width, CV_8UC3);
ifstream Filein(inputpath, ios::in | ios::binary);
Filein.seekg(0, ios::beg);//头
Start = Filein.tellg();//头地址
Filein.seekg(0, ios::end);//尾
End = Filein.tellg();//尾地址
Size = End - Start;
Filein.seekg(0, ios::beg);//头
if(!(Size % (Width*Hight)))
{
Filein.read((char *)buffer, Hight*Width * 2);
Filein.close();
for (int m = 0; m < Hight*Width; m += 2)
{
//YUYV
y1 = buffer[m * 2 + 0];
y2 = buffer[m * 2 + 2];
u = buffer[m * 2 + 1];
v = buffer[m * 2 + 3];

r1 = CYCbCr2R(y1, u, v);
g1 = CYCbCr2G(y1, u, v);
b1 = CYCbCr2B(y1, u, v);

rgbimg.data[m * 3 + 0] = b1;
rgbimg.data[m * 3 + 1] = g1;
rgbimg.data[m * 3 + 2] = r1;
r2 = CYCbCr2R(y2, u, v);
g2 = CYCbCr2G(y2, u, v);
b2 = CYCbCr2B(y2, u, v);
rgbimg.data[m * 3 + 3] = b2;
rgbimg.data[m * 3 + 4] = g2;
rgbimg.data[m * 3 + 5] = r2;
}
ofstream Fileout(outputpath, ios::out | ios::binary);//结果
imwrite(outputpath, rgbimg);
Fileout.close();
}
// imshow("【原始图】", rgbimg);

}

原文地址:https://www.cnblogs.com/fengliu-/p/7267578.html