retrieving strings from file

Please discuss general Delphi programming topics here.

retrieving strings from file

Postby mac1234mac » October 21st, 2008, 2:18 pm

Hello,

I'd like to learn how I could retrieve values of numbers and character
symbols from file, for example:
I have sequence of characters saved in txt file -
"numbers_and_symbols.txt":

abcd -1234.56 on

Particular parts of string are separated from each other by space.

I'd like to use, in Delphi program, string 'abcd' as string variable,
number -1234.56 as float variable
and 'on' as bool variable. How could I do it in Delphi 7.0?.

Cheers
mac1234mac
Active Member
Active Member
 
Posts: 5
Joined: July 24th, 2008, 7:17 am

Postby Kambiz » October 21st, 2008, 5:15 pm

There are some functions in SysUtils unit for converting strings to other data types. For example, StrToFloat converts a numerical string to double.

Of course, first you have to parse your text file to get each string token.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2421
Joined: March 7th, 2003, 7:10 pm

Postby mac1234mac » October 22nd, 2008, 6:55 am

But if I have string in a file, how can I load it to string variable in Delphi 7.0?. For example: I've got string 'abcd' placed in the first line of the
file named 'text.txt'. How can I retrieve it so I would have string
variable in program: s:='abcd'?.

Thank You for answer.
mac1234mac
Active Member
Active Member
 
Posts: 5
Joined: July 24th, 2008, 7:17 am

Postby Kambiz » October 22nd, 2008, 7:15 am

Look at this piece of code:

Code: Select all
var
  Lines: TStringList;
  I: Integer;
  Line: String;
begin
  Lines := TStringList.Create;
  try
    Lines.LoadFromFile('path\to\your\file.txt');
    for I := 1 to Lines.Count do
    begin
      Line := Lines[I];
      /*
        Here you have access to each line of your text file using the Line variable.
      */
    end;
  finally
    Lines.Free;
  end;
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2421
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests

cron