[PHP] 使用ftell和fseek函数直接定位文件位置获取部分数据
对于大文件只获取部分数据很有用
1.使用ftell函数可以获取当前指针的字节位置
2.使用fseek函数可以直接定位到指定的位置
3.读取指定字节的数据就可以部分获取文件内容了
<?php
class FileStream
{
private $fp = null;
private $mode = 'r';
private $context = null;
private $readonly = false;
private $writeonly = false;
private $appendMode = false; public function __construct($file, $mode = 'r', $context = null)
{
$mode = trim($mode);
if (isset($mode[0])) {
$this->mode = strtolower($mode);
} if ($context) {
$this->context = $context;
$this->fp = fopen($file, $mode, false, $this->context);
} else {
$this->fp = fopen($file, $mode);
} if (!$this->fp) {
throw new Exception('can not open ' . $file);
} if ($this->mode == 'r') {
$this->readonly = true;
} elseif ($this->mode == 'w') {
$this->writeonly = true;
} elseif ($this->mode[0] == 'a') {
$this->appendMode = true;
}
} public function __destruct()
{
$this->close();
} public function close()
{
if (!$this->fp) {
fclose($this->fp);
$this->fp = null;
}
} public function read($size)
{
if ($this->writeonly) {
throw new Exception('write only');
} if (!$this->fp) {
throw new Exception('stream already closed');
} $buf = fread($this->fp, $size);
if ($buf === false) {
throw new Exception('read failed');
} return $buf;
} public function readLine()
{
if ($this->writeonly) {
throw new Exception('write only');
} if (!$this->fp) {
throw new Exception('stream already closed');
} return fgets($this->fp);
} public function readAll()
{
if ($this->writeonly) {
throw new Exception('write only');
} if (!$this->fp) {
throw new Exception('stream already closed');
} $buf = ''; while (true) {
$s = fread($this->fp, 8192);
if ($s === false) {
throw new Exception('read failed');
} if (!isset($s[0])) {
break;
} $buf .= $s;
} return $buf;
} public function write($data)
{
if ($this->readonly) {
throw new Exception('read only');
} if (!$this->fp) {
throw new Exception('stream already closed');
} if (fwrite($this->fp, $data) === false) {
throw new Exception('write failed');
}
} public function tell()
{
if ($this->appendMode) {
throw new Exception('tell can not work on appendmode');
} if (!$this->fp) {
throw new Exception('stream already closed');
} $p = ftell($this->fp);
if ($p === false) {
throw new Exception('tell failed');
} return $p;
} public function seek($position)
{
if ($this->appendMode) {
throw new Exception('seek can not work on seekmode');
} if (!$this->fp) {
throw new Exception('stream already closed');
} if (fseek($this->fp, $position) !== 0) {
throw new Exception('seek failed');
}
}
} $stream=new FileStream("1.log");
$start=0;
$end=0;
//获取开始和结束的字节位置
while($ln=$stream->readLine()){
if($ln=="3333333333333\r\n"){
$start=$stream->tell();
}
if($ln=="5555555555555\r\n"){
$end=$stream->tell();
}
}
var_dump($start,$end); //直接定位到开始的字节位置
$stream->seek($start);
//读取指定字节数的数据
$res=$stream->read($end - $start); var_dump($res);
1.log的内容
2.获取部分结果
[PHP] 使用ftell和fseek函数直接定位文件位置获取部分数据的更多相关文章
- php使用file函数、fseek函数读取大文件效率分析
php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下. 1. 直接 ...
- Eclipse 进入代码定位文件位置
- shell 字符串中定位字符位置 获取字符位置
linux shell 字符串操作(长度,查找,替换)详解 该博文中描述的如下两个字符串操作, ${string:position} #在$string中, 从位置$position开始提取子串 ${ ...
- 文件操作:fseek函数和ftell函数
1.fseek函数: int fseek(FILE * _File, long _Offset, int _Origin); 函数设置文件指针stream的位置.如果执行成功,stream将指向以fr ...
- 如何通过ftell和fseek来获取文件大小
#include <stdio.h> int main () { FILE * pFile=NULL; long size; pFile = fopen ("myfile.txt ...
- PHP fseek() 函数
定义和用法 fseek() 函数在打开的文件中定位. 该函数把文件指针从当前位置向前或向后移动到新的位置,新位置从文件头开始以字节数度量. 如果成功该函数返回 0,如果失败则返回 -1.请注意,移动到 ...
- C中的fseek函数使用
函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式: int fseek(FILE *stream, long ...
- PHP移动文件指针ftell()、fseek()、rewind()总结
在对文件进行读写过程中,有时需要在文件中跳转.同不同位置读取,以及将数据写入到不同的位置.例如,使用文件模拟数据库保存数据,就需要移动文件指针.指针的位置是以从文件头开始的字节数度量的,默认以不同模式 ...
- strtok()函数、fseek()函数、fwrite()函数、fread()函数的使用
在电子词典这个项目过程中遇到了几个主要的问题,第一个是怎么解决把翻译分开这个.第二个事情就是怎么把结构体写到文件中.这两个问题,一个是关于字符串的操作一个是关于文件的操作. strtok函数 char ...
随机推荐
- 堆与栈(heap and stack)在c/c++的应用(概念)
在学习c/c++时,我们经常会遇到 堆与栈 的问题,今天就来讲一下各类情况下的heap,stack的应用. 程序内存布局场景下,堆与栈表示两种内存管理方式: 1.内部分配时,堆和栈表示两种不同的内存管 ...
- 第50 课C++对象模型分析——成员变量(上)
C++对象模型,其实就是C++中的对象在内存中是如何排布的.C++中的对象包含了成员变量和成员函数,其实就是研究C++中的类对象它的成员变量和成员函数在内存中是如何排布的. 回归本质class 是一种 ...
- 20K掌握的技术要点?
银四指的是每年的三四月份都是人才招聘的高峰期,由于跟新年和春运紧接,到人才市场,人都是满的,所以称为 :伴随的四月则称为银四.每一年职场迎来“ 银四”.总结做完了,得失看清了,奖金拿到了,“算账”往后 ...
- Docker 简单发布dotnet core项目 文本版
原文:https://www.cnblogs.com/chuankang/p/9474591.html docker发布dotnet core简单流程 照着步骤来基本没错 但是有几个要注意的地方: v ...
- CF1207G Indie Album
题目链接 problem 有\(n\)个字符串,对于第\(i\)个字符串通过以下两种方式中的一个给出. \(1\; c\),该字符串只含一个字符\(c\). \(2\ x\ c\),该字符串为第\(x ...
- Luogu P4341 [BJWC2010]外星联络
题目描述 暴力思路: 暴力枚举子串,插入trie树中,按字典序输出大于1的end的值 时间复杂度:n3 结果:TLE 0分 ...非常尴尬的一点不剩 正解思路 经过观察发现,可以只枚举后缀,统计时输出 ...
- vue使用--saas的引入与使用
什么是saas.scss? saas是一种动态样式语言,属于CSS预处理器,为CSS增加了一些编程特性,比如变量.嵌套.函数.继承.运算等等.开发人员可以像使用js等语言一样使用saas进行css的 ...
- 公式推导【BACF//ICCV2017】
HK Galoogahi, A Fagg, S Lucey. Learning Background-Aware Correlation Filters for Visual Tracking[C]. ...
- 《一起学mysql》1
自从达内毕业后就没用过mysql,一直用的hive,hdfs 存储数据,最近突然又接触到了关系型数据库.本想随便从网上 找个教程看看,但是都不是很满意,pdf看着又难受,还是自己个儿写个笔记吧. ...
- HBase的java操作,最新API。(查询指定行、列、插入数据等)
关于HBase环境搭建和HBase的原理架构,请见笔者相关博客. 1.HBase对java有着较优秀的支持,本文将介绍如何使用java操作Hbase. 首先是pom依赖: <dependency ...