这是获取和删除文件第一行的最有效方法吗? [英] Is this the most efficient way to get and remove first line in file?

查看:80
本文介绍了这是获取和删除文件第一行的最有效方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,每次调用该脚本都会获取文件的第一行.已知每一行的长度完全相同(32个字母数字字符),并以"\ r \ n"结尾. 获取第一行后,脚本将其删除.

I have a script which, each time is called, gets the first line of a file. Each line is known to be exactly of the same length (32 alphanumeric chars) and terminates with "\r\n". After getting the first line, the script removes it.

这是通过以下方式完成的:

This is done in this way:

$contents = file_get_contents($file));
$first_line = substr($contents, 0, 32);
file_put_contents($file, substr($contents, 32 + 2)); //+2 because we remove also the \r\n

显然可以,但是我想知道是否有更聪明(或更有效)的方式来做到这一点?

Obviously it works, but I was wondering whether there is a smarter (or more efficient) way to do this?

在我的简单解决方案中,我基本上读取并重写整个文件只是为了删除第一行.

In my simple solution I basically read and rewrite the entire file just to take and remove the first line.

推荐答案

除了重写文件,没有比这更有效的方法了.

There is no more efficient way to do this other than rewriting the file.

这篇关于这是获取和删除文件第一行的最有效方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆