Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search for a string in a text file 1

Status
Not open for further replies.

LEICJDN1

Technical User
Nov 27, 2002
201
GB
Hello again. More help required I am afraid.

My program outputs results to a text file, seperated by commas for later analysis in Excel.

Example:

10001,128,32,400,50

The first number is the unique identifier for each record in the file.
I use Append, Write, Writeln, flush and close to write the results file.

How can I check that the identifier does not already appear in the file before writing the results to avoid duplication / or allow checking / overwriting?

If this is possible, how can I position the write point to overwrite the entry if appropriate?

Thanks.
 
Use a stringlist to store your existing unique values and check each new unique value using the stringlist.indexof function.
Assuming that the output text file is empty at the beginning of processing, the following code shoudl be enough...

{ THESE LINE RUN BEFORE YOU START PROCESSING DATA }
sl := tstringlist.create;
sl.sorted := true;
try
:
:
{ THESE LINES SHOULD RUN FOR EACH DATA ELEMENT PROCESSED }
if sl.indexof(inttostr(unique_key)) = -1
then begin
sl.add(inttostr(unique_key));
{ unique_key has never been used before }
end
else begin
{ unique_key has been used before }
end;
:
:
{ THESE LINES RUN WHEN YOU ARE DONE PROCESSING DATA }
finally
sl.free;
end;
If it's stupid but it works, it isn't stupid
 
Thanks, I can see the logic behind that. However, I have never used a string list before and am unsure how to load from a text file into a string list to run the above code.

Thanks.
 
Ok.
If you have a pre-existing text file and you are adding new data to it, you will need to load it's existing unique ids into the stringlist before you start. (Notice that we are only storing the unique ids in the stringlist. If we store the complete 5 integer csv strings from the text file, the the indexof method would only hit when all 5 integer's were non-unique.)
Here is how to load the stringlist under these conditions...

{ THESE LINE RUN BEFORE YOU START PROCESSING DATA }
sl := tstringlist.create;
sl.sorted := true;
try
if fileexists('myfile.csv')
then begin
assign(f,'myfile.csv');
reset(f);
i := ioresult;
if i = 0
then begin
while i = 0
do begin
readln(f,s);
i := ioresult;
if (i = 0) and (pos(',',s) > 0)
then begin
unique_key := copy(s,1,pos(',',s)-1);
sl.add(unique_key);
end;
end;
closefile(f);
i := ioresult;
end;
end;
:
:
{ THESE LINES SHOULD RUN FOR EACH DATA ELEMENT PROCESSED }
if sl.indexof(inttostr(unique_key)) = -1
then begin
sl.add(inttostr(unique_key));
{ unique_key has never been used before }
end
else begin
{ unique_key has been used before }
end;
:
:
{ THESE LINES RUN WHEN YOU ARE DONE PROCESSING DATA }
finally
sl.free;
end;

I strongly recommend your reading the help files on tstringlists! They are a very powerful feature of Delphi that I use almost everyday for little utility tasks like this.
Peace,
Colt. If it's stupid but it works, it isn't stupid
 
Thanks,

Will work through that. Will read all the helpfile on stringlists, and also work throught your code, as I find that the best way to figure things out.

Regards.
 
Good Addition BTecho!

I assumed that LEICJDN1 had a large number of additions to perform (in which case getting the existing unique keys into memory was more efficient). If only a few additions will be done at any given time, it would be cleaner and less limiting to search the file on the harddrive for each addition.
One note about searching the file on the harddrive: Since the unique key is the first field in any given line when reading the file as a text file, you will need to add a #10 or #13 character to the beginning of the string you are searching for. This will keep you from encountering false positives when the searched for integer value is present in one of the four non-key data elements on each line.

Peace,
Colt. If it's stupid but it works, it isn't stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top