利用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 ", getFrameTotalYuv(file)); return 0; }
结果为:300
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: ThinkPhp插入数据到数据库
- 下一篇: PHP 大图片操作导致内存崩溃及超时的问题