我如何知道文件资源当前“位于”哪一行?在 PHP 中?

发布于 2024-08-03 21:36:05 字数 303 浏览 1 评论 0原文

使用 PHP,可以使用 fopenfgets 读取文件的内容。每次调用 fgets 时,它都会返回文件中的下一行。

fgets 如何知道要读取哪一行?换句话说,它如何知道它上次读取的是第 5 行,所以这次应该返回第 6 行的内容?有没有办法让我访问该行号数据?

(我知道可以通过使用 file 将文件的全部内容读入数组来完成类似的操作,但我想使用 fopen 来完成此操作。)

Using PHP, it's possible to read off the contents of a file using fopen and fgets. Each time fgets is called, it returns the next line in the file.

How does fgets know what line to read? In other words, how does it know that it last read line 5, so it should return the contents of line 6 this time? Is there a way for me to access that line-number data?

(I know it's possible to do something similar by reading the entire contents of the file into an array with file, but I'd like to accomplish this with fopen.)

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

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

发布评论

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

评论(4

溇涏 2024-08-10 21:36:05

每个打开的文件在内存中都会保存一个“位置”;每次您从文件中读取一行/字符/任何内容时,它都会自动更新。

您可以通过 ftell 获取此位置,并修改它与 fseek

ftell — 返回当前位置
文件读/写指针

fseek — 搜索文件指针

您还可以使用 倒带 ...倒回...该指针的位置。

这并不是让你得到一个行号的位置,而是更接近一个字符号的位置(实际上,你得到的位置是从文件开头开始的字节数);当你有了它时,读取一行只是读取字符的一米,直到你遇到行尾字符。

顺便说一句:据我所知,这些函数来自 C 语言——PHP 本身就是用 C 编写的;-)

There is a "position" kept in memory for each file that is opened ; it is automatically updated each time you are reading a line/character/whatever from the file.

You can get this position with ftell, and modify it with fseek :

ftell — Returns the current position
of the file read/write pointer

fseek — Seeks on a file pointer

You can also use rewind to... rewind... the position of that pointer.

This is not getting you a position as a line number, but closer to a position as a character number (actually, you are getting the position as a number of bytes from the beginning of the file) ; when you have that, reading a line is just a metter of reading characters until yu hit an end of line character.

BTW : as far as I remember, these functions are coming from the C language -- PHP itself being written in C ;-)

神经暖 2024-08-10 21:36:05

文件只是一个数据流,从头到尾读取。操作系统会记住您迄今为止在该文件中读取的位置。如果需要,在应用程序中执行此操作也相当简单。不过,操作系统只关心字节位置,而不关心行。

想象一下按顺序分发一副 52 张牌。你递出第一张卡片。下次2.卡。当你想分发第三张卡片时,你不需要从头开始数,甚至不需要记住你在哪里,你只需分发下一张可用的卡片,那就是第三张。

读取行可能需要做更多的工作,因为出于性能考虑,您希望缓冲从实际文件读取的数据,但这并不比记录您最后一条数据的偏移量多多少。分发,找到下一个换行符并传递这两个点之间的所有数据。

PHP 和操作系统都没有真正需要保留行号,因为所有系统关心的是“下一行”。如果您想知道行号,您可以保留一个计数器,并在每次应用程序读取行时递增它。

$lineno=0;
while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    lineno++; // keep track of the line number
     ...

}

Files are just a stream of data, read from the beginning to the end. The OS will remember the position you've read so far in that file. If needed, doing so in the application as well is fairly simple. The OS only cares about byte positions though, not lines.

Just imagine dealing out a deck of 52 card sequentially. You hand off the first card. Next time the 2. card. When you want to give out the 3. card , you don't need to start counting from the start again, or even remembering where you were you just hand out the next available card, and that'll be the third.

It might be a bit more work that's needed to read lines, since you'd want to buffer data read from the actual file for preformance sake, but it's not that much more to it than to record the offset of the last piece of data you handed out, find the next newline character and hand off all the data between those 2 points.

PHP nor the OS has no real need to keep the line number around, since all the system care about is "next line". If you want to know the line number, you keep a counter and increment it every time your app reads a line.

$lineno=0;
while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    lineno++; // keep track of the line number
     ...

}
枉心 2024-08-10 21:36:05

我有这个旧样本,我希望它可以帮助你:)

$File = file('path');
$array = array();

$linenr = 5;

foreach( $File AS $line_num => $line )
{
$array = array_push( $array , $line );
}

echo $array[($linenr-1)];

i hav this old sample i hob its can help you :)

$File = file('path');
$array = array();

$linenr = 5;

foreach( $File AS $line_num => $line )
{
$array = array_push( $array , $line );
}

echo $array[($linenr-1)];
我恋#小黄人 2024-08-10 21:36:05

您只需调用 fgets 并在每次调用时递增 var $line_number 即可。这会告诉你它所在的线路。

You could just call fgets and increment a var $line_number each time you call it. That would tell you the line it is on.

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