如何仅将tstringlist中的特定项目保存到文件 [英] How to save only a particular item in the tstringlist to a file
问题描述
这是我的代码。
var
filehan : Textfile;
i : Integer;
LineOfText : String;
tsl : TStringList;
Name, Emp_id : string;
begin
stf := TStringList.create;
Assignfile ( filehan, 'EMP.txt');
Reset ( filehand );
While not EOF(filehan) do
begin
inc(i);
ReadLn ( filehan, LineOfText );
tsl :=substrings(LineOfText, ':' );
Name := tsl[1];
Emp_id := tsl[0];
tsl.SaveToFile('FSTRING.txt');
end;
CloseFile (FFile);
end;
function SubStrings(AString: String; ADelimiter: Char): TStringList;
var
sList : TStringList;
curPos : Integer;
subString : String;
{ DelimiterPos : Integer}
begin
curPos := 1;
sList := TStringList.Create;
while (curPos <= Length(AString)) do
begin
subString := '';
While (AString[curPos] <> ADelimiter) and
(curPos <= Length(AString)) do
begin
subString := subString + Copy(AString,curPos,1);
curPos := curPos + 1;
end;
sList.Add(subString);
curPos := curPos + 1;
end;
Result := sList;
end;
文件包含大约2000行。
一行如下
The file consist of around 2000 lines. one line looks like this
300: rani : joseph: 210: 500 : 700
如果该行在第3位有数字210,我必须检索存储在数据库中的员工的更多详细信息。我该怎么办?
If the line has the number 210 at 3rd position,I have to retrieve more details of the employee which is stored in database. How I can do it?
我想读取文件中的所有行。
I want to read all the lines in the file.
谢谢您的时间。
推荐答案
Don'别忘了从tsl回收内存,这样它就不会泄漏。
似乎您来自某种垃圾收集语言,懒惰的地方您根本不考虑内存,例如PHP或Python。
It seems you're came from some garbage-collected language, lazy oen where u do not think about memory at all, like PHP or Python.
因此,您最好阅读有关对象及其寿命的Delphi帮助,或阅读一些有关liphi Delphi Foundations的书。
So you'd better read Delphi help about objects and their life, or read some book liek Delphi Foundations.
直到…………变体3:由于您似乎不具备控制和管理对象生命周期的技能,因此请使用引用计数类型,例如数组或接口,Delphi或多或少地控制着它们的生命周期。
Until that... Variant 3: Since you seems not having the required skill to control and manage lifetime of objects, then use reference-counted types, like arrays or interfaces, for which Delphi controls their lifetime more or less.
从基于分隔符将字符串拆分为字符串数组,您可以草拟一些变体,例如:
Elaborating from the answers from Split a string into an array of strings based on a delimiter you can draft few variants, for example:
var sda: TStringDynArray;
begin
sda := SplitString(LineOfText, ':' );
Assignfile ( filehan2, 'FSTRING.txt');
Rewrite ( filehand2 );
try
WriteLN (filehan2, sda[0]); WriteLN (filehan2, sda[1]);
finally
CloseFile(filehan2);
end;
end;
好吧-因为您告诉您有老的Delphi 7-您那里没有SplitString函数。但是您可以花几分钟时间来完成一个。 http://pastebin.ca/2309695
Okay - since you told you have old Delphi 7 - you do not have SplitString function there. But you can spare few minutes and make one. http://pastebin.ca/2309695
您还可以从 http://jcl.sf.net
var isl1, isl2: IJclStringList;
begin
isl1 := TJclStringList.Create;
isl1.LoadFromFile('EMP.txt');
isl2 := TJclStringList.Create.Split(isl1[0], ':');
isl1.Clear.Add( [ isl2[0], isl2[1] ] ).SaveToFile('FSTRING.txt');
end;
使用示例 300:rani:joseph:210:500:700
看来您在真实数据周围有很多空白。
然后您应该修剪这些空格。像 WriteLN(filehan2,Trim(sda [0]));
或像 isl1.Clear.Add([Trim(isl2 [0]), .....
阅读有关 Trim
函数的手册;
With the sample like 300: rani : joseph: 210: 500 : 700
it seems you have a lot of spaces around real data.
Then you should trim those spaces off. Like WriteLN (filehan2, Trim( sda[0] ));
or like isl1.Clear.Add( [ Trim( isl2[0] ),.....
Read manuals about Trim
function;
如果该行的编号为210
If the line has the number 210
然后检查它,只是带有 if语句;
Then check it, just with "if" statement;
var isl1, isl2, isl3: IJclStringList;
EveryLine: string; i: integer;
begin
isl1 := TJclStringList.Create;
isl2 := TJclStringList.Create;
isl3 := TJclStringList.Create;
isl1.LoadFromFile('EMP.txt');
// for EveryLine in isl1 do begin
// - this works in free Lazarus or modern Delphi, but not in D7 }
for i := 0 to isl1.Count - 1 do begin;
EveryString := isl1[i];
isl2.Split(EveryString, ':').Trim;
if isl2.Count >= 4 then // does 3rd element even exist ???
if StrToIntDef( isl2[3], -1 ) = 210 then
CallSomeProcedureToRetrieveMoreDetails;
isl3.Clear.Add( [ isl2[0], isl2[1] ] ).SaveToFile('FSTRING.txt');
end; // for
end; // function
这篇关于如何仅将tstringlist中的特定项目保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!