当前位置:主页>销售管理软件> 列表

打开一个.txt文件,如何实现一行一行的读取? 找管家婆软件下载

库存管理软件版1楼: 打开一个.txt文件,能否实现文本文件的一行一行的读取??

2楼: 使用readln即可 如用友财务软件官网

3楼: procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
FileList : TStrings;
begin
FileList := TStringList.Create;
try
FileList.LoadFromFile(''C:\Log.txt'');
for i := 0 to FileList.Count -1 do
begin
ShowMessage(FileList.Strings[i]);
end;
finally
FileList.Free;
end;
end;

4楼: f:textfile;
str:string
assignfile(f,filename);//filename为文件路径
reset(f);
while not eof(f) do
begin
readln(f,str);
////你的操作代码//////
end;
closefile(f);

5楼: 如royal1442所说,不过我觉得用TStringList似乎好些,他的LoadFromFile可以直接将文本的每一行作为一项添加到List中,操作方便

6楼: function FileToString(const FilePath: string): string;
var
hFile: THandle;


nSize: DWord;
begin
Result := '''';

hFile := CreateFile(PChar(FilePath), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile = INVALID_HANDLE_VALUE) then Exit;

nSize := GetFileSize(hFile, nil);

SetLength(Result, nSize);
ReadFile(hFile, Result[1], nSize, nSize, nil);

CloseHandle(hFile);
end;

function StringToFile(const FilePath, FileText: string): Boolean;
var
hFile: THandle;
nSize: DWord;
begin
Result := False;

hFile := CreateFile(PChar(FilePath), GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile = INVALID_HANDLE_VALUE) then Exit;

WriteFile(hFile, FileText[1], Length(FileText), nSize, nil);

SetEndOfFile(hFile);
CloseHandle(hFile);

Result := True;
end;

库存管理软件版7楼: 同意royal1442,
不过最好先判断一下文件是否存在

8楼: 一般情况 都是用royal1442,方法操作。

9楼: 用TStringList来读最简单!


var
ff:TStringList;
filename:string;
begin
filename:=''file.txt'';
ff:=TStringList.Create;
if fileExists(filename) then
begin
ff.loadfilename(filename);
writeln(''文件总行数:''+inttostr(ff.count));
writeln(''文件第1行的内容:''+ff.strings[0]);
if (ff.count>0) then
writeln(''文件最后1行的内容:''+ff.strings[ff.count-1]);
end;
ff.free;
end;

10楼: 我這有一個例子,你可以試試,這是一個字段一個字段的讀寫。

procedure TForm1.Button1Click(Sender: TObject);
var
strs: TStrings;
i, j: integer;
f: textfile;
s: string;
begin
Assignfile(F, edit1.text);
reset(f);
readln(f, s);
strs := TStringList.Create;
strs.Delimiter := '';'';
strs.DelimitedText := s;
j := 1;
while not eof(f) do begin
for i := 0 to Strs.Count - 1 do
stringgrid1.Cells[i, j] := strs[i];
//ShowMessage(Strs[i]);
stringgrid1.RowCount := stringgrid1.RowCount + 1;
readln(f, s);
strs.DelimitedText := s;
inc(j);
end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if opendialog1.Execute then
edit1.Text := opendialog1.FileName;
end;


樓主記的給分喲。

11楼: 麻子,能不能给些简短实用的阿,并不是所有的人的api基础都像你那么好啊

12楼: 我也觉得用TStringList好 如管家婆软件下载

13楼: 同意royal1442