如题
解决方案 »
- delphi如何模拟 鼠标滚轮往下的操作
- D2010用TPerlRegEx组件的问题
- TIDTcpServer的writeln函数为什么会出现I/O 105 ERROR错误?
- 仿Winamp做个播放器,请教几个问题
- 在IntraWeb技术中I我创建了3个网页,第一个为form1,第二个为form2,第三个为form3,在运行时程序默认为第一个,我想让第二个网也成为首先
- 还是quickreport的问题::subdetail
- Delphi中拖动光标Cursor怎么设置?
- 在线等待……谁先谁得分!
- 如何实现套打发票
- 同志们,妹妹我比较笨,闹不明白什么叫木马,木马是什么呀?
- idTcpClient、Server问题,、急,百分相赠
- 可以弹出菜单的按钮是怎么做的?
S: string;
begin
if OpenDialog1.Execute then
begin
AssignFile(F, OpenDialog1.FileName);
Reset(F);
Readln(F, S);
Edit1.Text := S;
CloseFile(F);
end;
end;
begin
F:= TStringList.Create;
F.Loadfrom("文件名");
...
F.strings[n].... // n 代表行号,从0开始
楼上的也不算,因为是调入了整个文件啦
var Lines:Tstrings;
begin
lines:=TStringList.Create;
lines.LoadFromFile('');
end;
var
sTxt: TextFile;
s: string;
line: TStringList;
begin
line := TStringList.Create;
AssignFile(sTxt, 'D:\01.txt'); //假设文件存在
Reset(sTxt);
while not eof(sTxt) do
begin
Readln(sTxt, s);
line.Add(s);
end;
ShowMessage(line.Strings[Random(line.Count)]);
end;
微软的记事就是这样的,其实word也是调入好多页的
所以当打开比较大的文件的时候就会很慢,尤其是记事本
如果你想随便调入某行,那就不如不是用文本形式的文件
使用记录文件了,每行条记录,这样你就可以控制文件指针指向你所要的那条记录(也就是
你想要的那行)
var
hMap: THandle;
hFile:integer;
MapFp: Pointer ;
fs:DWORD;
i,LineNo,FirstPtr,len:integer;
begin
hFile:=FileOpen(fn,fmOpenReadWrite);
if hFile<0 then
begin
ShowMessage('file open fail!');
exit;
end;
fs:=GetFileSize(hfile,Nil);
hMap:=CreateFileMapping(hFile,nil, page_ReadWrite, 0,0,nil);
if hMap=0 then
begin
FileClose(hFile);
ShowMessage('map file open fail!');
exit;
end;
MapFp:=MapViewOfFile(hMap,File_Map_All_Access,0, 0, 0);
if MapFp = nil then
begin
CloseHandle(hMap);
FileClose(hFile);
ShowMessage ('MapFile data pointer failed');
exit;
end;
lineno:=0;
i:=0;
FirstPtr:=0;
while (i<fs) do
begin
if PChar(Mapfp)[i]=#10 then
begin
inc(lineno);
if lineno=a_lineno then
begin
len:=i-FirstPtr-1;
if len>0 then
begin
SetLength(ReSult,len);
Move(PChar(Mapfp)[FirstPtr],Result[1],len);
end;
end else
FirstPtr:=i+1;
end;
inc(i);
end;
UnmapViewOfFile(MapFp);
CloseHandle(hMap);
FileClose(hFile);
end;