C 讀取超大文字檔案

2021-09-13 18:48:08 字數 999 閱讀 5763

現有乙個超大txt檔案,內部以\t分格,每行244個值,一共250萬行,大小1.9g,第一行是列名,現在要將每一行的資料讀出來進行處理,如果一次性讀進記憶體肯定是不行的。

目錄

讀第一行:

讀後續行:

遍歷方法

使用感受

參考:

string headerline = file.readlines(path).firstordefault(); // read the first row for headings
_filepath是檔案路徑

_currentline是讀過的行數

limit指定的是一次讀多少行

_encoding指定編碼為gbk或utf-8

static private system.text.encoding _encoding = encoding.getencoding("gb2312");

public static ienumerablereadmultilines(int limit = 100)

catch(exception )

}

readlines返回的是乙個ienumerable型別,可以用iter進行遍歷。

iter.current直接獲取每一行的字串,不可以用iter.tostring(),會很費時間。

var lines = file.readlines(_filepath, _encoding).skip(_currentline).take(bigstep);

var iter = lines.getenumerator();

int size = lines.count();

while (iter.movenext())

每次讀500行,在讀取前100萬行時感覺還非常快,但在後面感覺還是有一點的延時。不過應該滿足需求了。

1.2.

3.

Java讀取超大文字檔案

近日由於相關業務,需要讀取乙個將近1g的檔案,然後將符合條件的資料insert進資料庫。而執行完程式之後,最後寫入資料庫的資料是將近100w條,在linux系統下執行時間將近3個小時,操作量還是上了乙個相當的規模。由於之前沒有進行過超大檔案的讀寫,一開始以為需要使用分布式系統等複雜的操作才能進行。後...

C 讀取文字檔案

很多初學c 的同學,對於讀取文字檔案,並按照行處理資料總是有點不知如何開始,作為c 的初學者,自己在這裡做一點筆記。其實利用c 按行讀取文字檔案其實很簡單。假設有乙個文字檔案,如下所示 1 2 3 2 3 4 3 4 5 5 6 7 7 8 9 檔名為split.txt 目的 按照行讀取資料,並乙個...

讀取文字檔案

void ctestdlg onreadinfo cfile filewrite1 testwrite1.txt cfile modecreate cfile modewrite cfile filewrite2 testwrite2.txt cfile modecreate cfile modew...