逐行读取大文件在线或存储阵列的线 [英] Reading a large file line by line or storing its lines in an array

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

问题描述

我有一个很大的文件,100,000行。我可以读每一行,并对其进行处理,或者我可以行存储在一个数组,那么处理它们。我想preFER用于额外的功能阵列,但我真的很担心与存储阵列中的许多线相关的内存使用情况,如果它是值得的。

I have a large file, 100,000 lines. I can read each line and process it, or I can store the lines in an array then process them. I would prefer to use the array for extra features, but I'm really concerned about the memory usage associated with storing that many lines in an array, and if it's worth it.

推荐答案

有两个功能,你应该熟悉。

There are two functions you should familiarize yourself with.

首先是 文件() ,该读取整个文件到一个数组,每行作为一个数组元素。这有利于缩短文件,可能不是你想要使用一个10万行的文件内容。这个函数处理自己的文件管理,让您无需显式地打开和关闭自己的文件。

The first is file(), which reads an entire file into an array, with each line as an array element. This is good for shorter files, and probably isn't what you want to be using on a 100k line file. This function handles its own file management, so you don't need to explicitly open and close the file yourself.

二是 与fgets() ,你可以用它来一次读取文件的一行。只要有更多的行过程中,你可以用它来环路,并运行循环中的行处理。你需要使用 fopen()函数来得到这个文件的句柄,您可能要跟踪的文件指针自己恢复管理(即让你不会有从头重新开始处理,如果出现横盘和脚本失败)等。

The second is fgets(), which you can use to read a file one line at a time. You can use this to loop for as long as there are more lines to process, and run your line processing inside the loop. You'll need to use fopen() to get a handle on this file, you may want to track the file pointer yourself for recovery management (i.e. so you won't have to restart processing from scratch if something goes sideways and the script fails), etc.

希望这足以让你开始。

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

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