如何读写文本文件中的指定一行(就这一点分了,行行好 (63分)

  • 主题发起人 主题发起人 二公子
  • 开始时间 开始时间

二公子

Unregistered / Unconfirmed
GUEST, unregistred user!
ss.txt
11111111 2222222222 3333333 4444444
33333333 2314243124 1413422 1241234
25235325 1234124144 1414124 1414144
.......
设文本文件较大,我要读其中的一行中含'77798789'(假设唯一)的字符串,将这一行存成数组

用什么方法做可以做到速度快,内存占用少呢?多谢各位了
 
从编程上说,这种方法最简单:
(如果着急的先用着,现在的机器硬件条件那么好,本地数据处理,速度不应该是问题。)
var
i: Integer;
begin
with TStringList.Create do
try
LoadFromFile('../ss.txt');
for i := 0 to Count - 1 do
begin
if pos('77798789',Strings) = 0 then Continue;
//Cope with what you want to do here
Break;
end;
finally
free;
end;
end;
 
读到Memo当中,然后查找77798789这个字符串。
 
//做到速度快,内存占用少
不可能,时间和空间不可能同时满足

//读到Memo当中,然后查找
千万别,否则你要后悔

你应该作出决定,到底要时间还是空间?然后我再帮你看看:)
 
to beta:
在较差机子上处理极快,内存占用尽可能降到最少
设文本文件的大小在5M以下
 
function ProcessFile(subString: String, FileName: String):Boolean;
var
F: System.TextFile;
S: String;
begin
AssignFile(F, FileName);
Reset(F);
while not Eof(F) do
begin
Readln(F, S);
if Pos(subString, F) <> 0 then
begin
{search sucessfully, encode your tasks here}
Result:=True;
break;
end
end
CloseFile(F);
Result:=False;
end;
一行一行的读入,然后判断当前行中是否含有目标串
 
后退
顶部