如何逐行读取大文件? [英] How to read a large file line by line?

查看:60
本文介绍了如何逐行读取大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐行读取文件,但没有将其完全加载到内存中.

I want to read a file line by line, but without completely loading it in memory.

我的文件太大,无法在内存中打开,如果尝试这样做,我总是会遇到内存不足的错误.

My file is too large to open in memory, and if try to do so I always get out of memory errors.

文件大小为1 GB.

推荐答案

您可以使用fgets()函数逐行读取文件:

You can use the fgets() function to read the file line by line:

$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
    }

    fclose($handle);
} else {
    // error opening the file.
} 

这篇关于如何逐行读取大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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