C++学习3:文件操作入门

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(FILE
stream,const char
format,[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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C基础上[2],一九八三年又由贝尔实验室的Bjarne Strou-strup推出了C++C++进一步扩充和完善了C语言,成为一种面向 对象的程序设计语言。C++目前流行的编译器最新版本是Borland C++ 4.5,Symantec C++ 6.1,和Microsoft Visual C++ 2012。C++提出了一些更为深入的概念,它所支持的这些面向对象的概念容易将问题空间直接地映射到程序空间,为程序员提供了一种与传统结构程序设计不同的思维方式和编程方法。因而也增加了整个语言的复杂性,掌握起来有一定难度。 C++由美国AT&T贝尔实验室的本贾尼·斯特劳斯特卢普博士在20世纪80年代初期发明并实现(最初这种语言被称作“C with Classes”带类的C)。开始,C++是作为C语言的增强版出现的,从给C语言增加类开始,不断的增加新特性。虚函数(virtual function)、运算符重载(Operator Overloading)、多重继承(Multiple Inheritance)、模板(Template)、异常(Exception)、RTTI、命名空间(Name Space)逐渐被加入标准。 C++ 1998年国际标准组织(international standard organization, ISO)颁布了C++程序设计语言的国际标准ISO/IEC 1988-1998。C++是具有国际标准的编程语言,通常称作ANSI/ISOC++。 1998年是C++标准委员会成立的第一年,以后每5年视实际需要更新一次标准。C++0x最终国际投票已于2011年8月10日结束,并且所有国家都投出了赞成票,C++0x已经毫无疑义地成为正式国际标准。先前被临时命名为C++0x的新标准将被称为C++ 2011。C++ 2011取代现行的C++标准ISO/IEC 14882,它公开于1998年并于2003年更新,通称C++98以及C++03。国际标准化组织于2011年9月1日出版发布ISO/IEC 14882:2011,名称是:Information technology -- Programming languages -- C++ Edition: 3。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值