利用fseek和ftell求文件的长度(字节数)

#include<iostream>
using namespace std;

int main()
{
	int offset;
	int fromWhere; // 2表示文件结尾

	FILE *fp = fopen("myData.txt", "w");
	fprintf(fp, "123456");
	fclose(fp);

	fp = fopen("myData.txt", "r");
	offset = 0;
	fromWhere = 2;
	fseek(fp, offset, fromWhere);
	cout << ftell(fp) << endl;
	fclose(fp);

	cout << "*********************" << endl;

	int a[10];
	memset(a, 0, sizeof(a));

	fp = fopen("yourData", "wb");
	fwrite(a, sizeof(a), 1, fp);
	fclose(fp);

	fp = fopen("yourData", "rb");
	fseek(fp, offset, fromWhere);
	cout << ftell(fp) << endl;
	fclose(fp);

	return 0;
}


      结果为:

6
*********************
40

 

     在视频中,经常要求视频的帧数,现有foreman_qcif.yuv文件,帧数为300,且看下面程序:(x264中就是这么求的)

#include <stdio.h>

// yuv文件结构体
typedef struct yuvFile
{
	FILE *fp;
	int width;
	int height;
}YUVFILE;

// 获取yuv文件的帧数
int getFrameTotalYuv(YUVFILE file)
{
    int frameTotal;
	int size;

    if( !fseek( file.fp, 0, SEEK_END ) )
    {
        size = ftell( file.fp );
        frameTotal = size/( file.width * file.height * 3 / 2 ); // yuv420
    }

    return frameTotal;
}

int main()
{
	YUVFILE file;

	file.fp = fopen("foreman_qcif.yuv", "rb");
	file.width = 176;
	file.height = 144;

	printf("%d\n", getFrameTotalYuv(file));

	return 0;
}

       结果为:300


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值