#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