0 概述
常用的文件函数及定义方式如下:
0.1 定义方式
FILE* file = NULL;
file = fopen("test.txt", "r")
FILE *open;
int error;
error = fopen_s(&open, "test.yuv", "rb")
0.2文件函数
FILE * fopen(const char * path, const char * mode);
errno_t fopen_s( FILE** pFile, const char filename, const char mode );
int fscanf(FILEstream,const charformat,[argument…]);
int fprintf (FILE* stream, const char*format, [argument]);
size_t fread ( void buffer, size_t size, size_t count, FILE stream) ;
size_t fwrite(const void buffer, size_t size, size_t count, FILE stream);
int fclose( FILE *fp );
0.3视频编码统计时最常用的部分
FILE* writepu = fopen("output.txt", "a");
fprintf(writepu,"(%d,%d)\n", pu.blocks[channelType].x, pu.blocks[channelType].y);
fclose(writepu);
writepu = NULL;
0.4示例
头文件如下:
/* read.h */
#include<stdio.h>
#include<stdlib.h>
void read_three_numbers();
void read_and_write_yuv_file();
/* write.h */
#include<stdio.h>
#include<stdlib.h>
void write_nine_numbers();
void write_yuv_file(unsigned char *imageOut_Y, unsigned char*imageOut_Cb, unsigned char*imageOut_Cr);
1 基本文件操作
/* read_three_numbers函数*/
void read_three_numbers()
{
FILE* read = NULL;//创建一个指向文件的指针
read = fopen("test.txt", "r");//fopen:打开文件,并确定打开类型,r只读
int a[3] = { 0 };
for (int i = 0; i < 3; i++)
fscanf(read, "%d", &a[i]);//fscanf:读取文件的内容
for (int i = 0; i < 3; i++)
printf("%d\n", a[i]);
fclose(read);//fclose:关掉文件
read = NULL;//清理指针
}
/* write_nine_number函数*/
void write_nine_numbers()
{
FILE *write = NULL;
write = fopen("test.txt", "w");
fprintf(write, "123\n");//fprintf:将文件的内容输出到文件中
fprintf(write, "456\n");
fprintf(write, "789\n");
fclose(write);
write = NULL;
}
2 视频编码常用文件操作
这里增加了对yuv的处理方式,即对二进制文件的处理:
/* read.cpp 完整版*/
#include<stdio.h>
#include<stdlib.h>
#include"read.h"
#include"write.h"
void read_three_numbers()
{
FILE* read = NULL;//创建一个指向文件的指针
read = fopen("test.txt", "r");//fopen:打开文件,并确定打开类型,r只读
int a[3] = { 0 };
for (int i = 0; i < 3; i++)
fscanf(read, "%d", &a[i]);//fscanf:读取文件的内容
for (int i = 0; i < 3; i++)
printf("%d\n", a[i]);
fclose(read);//fclose:关掉文件
read = NULL;//清理指针
}
void read_and_write_yuv_file()
{
FILE *open;
int error;
error = fopen_s(&open, "E:\\project\\0_266Test\\20190217 VTM4.0测试\\dec.yuv", "rb");//fopen_s:文件指针,文件位置,打开方式
if (error != NULL)
{
printf(" openning error\n");
}
unsigned char *imageIn_Y, *imageIn_Cb, *imageIn_Cr;//指向一片内存空间的指针
imageIn_Y = (unsigned char*)malloc(832 * 480);//开辟空间
imageIn_Cb = (unsigned char*)malloc(832 / 2 * 480 / 2);
imageIn_Cr = (unsigned char*)malloc(832 / 2 * 480 / 2);
fread(imageIn_Y, 832 * 480, 1, open);//fread:读取地址,读取数量,数据项所占字节数,文件指针
fread(imageIn_Cb, 832 / 2 * 480 / 2, 1, open);
fread(imageIn_Cr, 832 / 2 * 480 / 2, 1, open);
write_yuv_file(imageIn_Y, imageIn_Cb, imageIn_Cr);
//free(imageIn_Y);
//free(imageIn_Cb);
//free(imageIn_Cr);
fclose(open);
}
/* write.cpp 完整版*/
#include<stdio.h>
#include<stdlib.h>
#include"write.h"
void write_nine_numbers()
{
FILE *write = NULL;
write = fopen("test.txt", "w");
fprintf(write, "123\n");//fprintf:将文件的内容输出到文件中
fprintf(write, "456\n");
fprintf(write, "789\n");
fclose(write);
write = NULL;
}
void write_yuv_file(unsigned char *imageOut_Y, unsigned char*imageOut_Cb, unsigned char*imageOut_Cr)
{
FILE * out;
int errorout1;
errorout1 = fopen_s(&out, "Out_832x480.yuv", "wb");
printf("Out is good? %d", errorout1);
fwrite(imageOut_Y, 1, 832 * 480, out);//fwrite:写入位置的内存空间指针,每个数据项的字节数,数量,文件指针
fwrite(imageOut_Cb, 1, 832 / 2 * 480 / 2, out);
fwrite(imageOut_Cr, 1, 832 / 2 * 480 / 2, out);
fclose(out);//fclose函数
free(imageOut_Y);//释放空间!!!
free(imageOut_Cb);
free(imageOut_Cr);
}
/* main.cpp 完整版*/
#include"read.h"
#include"write.h"
int main()
{
char choose;
printf("Please choose! numbers for 'A' and yuv for 'B'\n");
scanf("%c", &choose);
if (choose == 'A')
{
write_nine_numbers();
read_three_numbers();
}
else if (choose == 'B')
{
read_and_write_yuv_file();
}
else
printf("error!");
system("pause");
return 0;
}