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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open a text file

Status
Not open for further replies.

troyu

Programmer
Nov 21, 2000
185
CA
How do I have a text file open from Delphi? What is the code that I need?
 
Easiest way is to create a stringlist and read the file into it -
For an example, put a memo on your form and a button, and in the on-click for the button put Memo1.Lines.LoadFromFile(filenamehere);

The harder (but sometimes more useful!) way is this:

procedure OpenFile();
var myFile: TextFile;
line: string;
begin
assignfile(myFile, filenamehere);
reset(myfile);
try
while not eof(myfile) do
begin
readln(myFile, line);
//do something with the newly read
//string here
end;
finally
closefile(myfile)
end;
end;
 
It depends of what type of file you want to read.
TealWren has the ultimate way to read raw text files, but if you want to read standard ini-files you should use TIniFile.

Example Code (You must implement IniFiles in the Uses clause:

function ReadIniFile(FileName: String; Section, Prop, Value, Default: String): String;
begin
with TIniFile.Create(Filename) do
try
Result := ReadString(Section, Prop, Default);
finally
Free;
end;
end;

Example of ini-file

[Section]
Property=Value



If you want to retrieve the value above, just call my function like this:

s := ReadIniFile('Section','Property','Default when not found');
 
TRegistry.ReadString

I am trying to read the (Default) value of the key .lay and the following does not work. Any suggestions?

I am trying the following:
Var RegistryDB : TRegistry;
aStringValue : String;
...
RegistryDB.RootKey := HKEY_CLASSES_ROOT; {working}
RegistryDB.OpenKey('.lay\'); {working}
aStringValue := RegistryDB.ReadString('(Default)'); {not working}

Thanks for any input.

 
Oops, Don't know how that happened. Ignore my reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top