fseek() 按行而不是字节?

发布于 2024-09-16 07:42:52 字数 289 浏览 7 评论 0原文

我有一个可以逐行解析大文件的脚本。当它遇到无法处理的错误时,它会停止,通知我们解析的最后一行。

这真的是寻找文件中特定行的最佳/唯一方法吗? (fseek() 在我的情况下不可用。)

<?php

for ($i = 0; $i < 100000; $i++)
    fgets($fp); // just discard this

我使用它没有问题,它足够快 - 只是感觉有点脏。根据我对底层代码的了解,我不认为有更好的方法来做到这一点。

I have a script that parses large files line by line. When it encounters an error that it can't handle, it stops, notifying us of the last line parsed.

Is this really the best / only way to seek to a specific line in a file? (fseek() is not usable in my case.)

<?php

for ($i = 0; $i < 100000; $i++)
    fgets($fp); // just discard this

I don't have a problem using this, it is fast enough - it just feels a bit dirty. From what I know about the underlying code, I don't imagine there is a better way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不疑不惑不回忆 2024-09-23 07:42:52

查找文件中特定行的一种简单方法是使用 SplFileObject 类,该类支持查找行号 (seek()) 或字节偏移量 (fseek())。

$file = new SplFileObject('myfile.txt');
$file->seek(9999);     // Seek to line no. 10,000
echo $file->current(); // Print contents of that line

在后台,seek() 只是执行 PHP 代码执行的操作(C 代码除外)。

An easy way to seek to a specific line in a file is to use the SplFileObject class, which supports seeking to a line number (seek()) or byte offset (fseek()).

$file = new SplFileObject('myfile.txt');
$file->seek(9999);     // Seek to line no. 10,000
echo $file->current(); // Print contents of that line

In the background, seek() just does what your PHP code did (except, in C code).

我不咬妳我踢妳 2024-09-23 07:42:52

如果您只有行号可以继续,则没有其他方法可以找到该行。文件不是基于行的(甚至不是基于字符的),因此无法简单地跳转到文件中的特定行。

可能还有其他读取文件中的行的方法可能会稍微快一些,例如将文件的较大块读入缓冲区并从中读取行,但您只能希望它能快几个百分点。任何在文件中查找特定行的方法仍然必须读取该行之前的所有数据。

If you only have the line number to go on, there is no other method of finding the line. Files are not line based (or even character based), so there is no way to simply jump to a specific line in a file.

There might be other ways of reading the lines in the file that might be slightly faster, like reading larger chunks of the file into a buffer and read lines from that, but you could only hope for it to be a few percent faster. Any method to find a specific line in a file still has to read all data up to that line.

一个人的旅程 2024-09-23 07:42:52

我知道发帖已经晚了,但它可以帮助一些人
有一天我做了一个像 fseekbyline 这样的函数...

function GoToLine($handle,$line)
{
  fseek($handle,0);  // seek to 0
  $i = 0;
  $bufcarac = 0;                    

  for($i = 1;$i<$line;$i++)
  {
    $ligne = fgets($handle);
    $bufcarac += strlen($ligne);  // in the end bufcarac will contains all caracters until the line
  }  

  fseek($handle,$bufcarac);
}

没有错误系统,如果你想转到行 <1 或 203 但文件是空的...
你不会得到任何好处。

如果你想离开eot也一样

I know it is late for posting but it can help some ppl
I did a function like fseekbyline one day ...

function GoToLine($handle,$line)
{
  fseek($handle,0);  // seek to 0
  $i = 0;
  $bufcarac = 0;                    

  for($i = 1;$i<$line;$i++)
  {
    $ligne = fgets($handle);
    $bufcarac += strlen($ligne);  // in the end bufcarac will contains all caracters until the line
  }  

  fseek($handle,$bufcarac);
}

there is no error system, if you wanna go to the line <1 or 203 but the file is empty ...
you will get nothing good.

same if you wanna go out of eot

绅士风度i 2024-09-23 07:42:52
rewind($handle);

for ($i=0; $i < $desired_line; $i++) {
    fgetcsv($handle, 1000, ",");
}

当我需要在脚本中多次倒带到特定行时,这对我有用。

我不确定这是否会消耗内存或速度,但它确实有效。

rewind($handle);

for ($i=0; $i < $desired_line; $i++) {
    fgetcsv($handle, 1000, ",");
}

This is working for me while I need to rewind to a specific line multiple times in my script.

I am not sure if this eats up memory or speed, but it does the trick.

一场信仰旅途 2024-09-23 07:42:52

如果我理解正确的话,您想在发现错误后的某个时刻寻找特定的行。如果是这种情况,您可能会在某处存储或打印坏行的行号,具体取决于“通知”的含义。

除非您真的意味着您不能使用fseek()*,否则您可以做的就是存储/打印文件中坏行开始的位置。然后您可以fseek()

* 在这种情况下,如果存在fseekbyline(),它如何可用?

If I understand correctly, you want to seek to the specific line at some point after you have found an error. If that is the case, you probably store or print the line-number of the bad line somewhere, depending on what you mean by "notify".

Unless you really mean that you cannot use fseek()*, what you can do is to also store/print the position in the file where the bad line starts. Then you can fseek().

* How, in that case, would fseekbyline() be usable if it existed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文