Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to read specific line from txt file in delphi?

err0rerr0r Posts: 7Member
I'm total begginer so let's begin =)
for example i want to read specific line from a txt file, how do i do that!?!?!
for example Code:

AssignFile(Somefile, 'somefile.txt');
Reset(Somefile);
// how to read line??? //
Closefile(somefile);

i was trying to findd something on internet, maybe there were correct answers but i didn't understand them... so if anyone knows, HELP ME and with some explanations would be better...

Comments

  • zibadianzibadian Posts: 6,349Member
    : I'm total begginer so let's begin =)
    : for example i want to read specific line from a txt file, how do i do that!?!?!
    : for example Code:
    :
    : AssignFile(Somefile, 'somefile.txt');
    : Reset(Somefile);
    : // how to read line??? //
    : Closefile(somefile);
    :
    : i was trying to findd something on internet, maybe there were correct answers but i didn't understand them... so if anyone knows, HELP ME and with some explanations would be better...
    :
    You can use ReadLn() to read a line from a text file. The first ReadLn() reads the first line, second ReadLn() the second, etc.
    Alternatively you can create a TStringList object and call its LoadFromFile(). This will store the entire fill into the object. Then you can use the Strings[] property to get the line you want.
  • err0rerr0r Posts: 7Member
    : : I'm total begginer so let's begin =)
    : : for example i want to read specific line from a txt file, how do i do that!?!?!
    : : for example Code:
    : :
    : : AssignFile(Somefile, 'somefile.txt');
    : : Reset(Somefile);
    : : // how to read line??? //
    : : Closefile(somefile);
    : :
    : : i was trying to findd something on internet, maybe there were correct answers but i didn't understand them... so if anyone knows, HELP ME and with some explanations would be better...
    : :
    : You can use ReadLn() to read a line from a text file. The first ReadLn() reads the first line, second ReadLn() the second, etc.
    : Alternatively you can create a TStringList object and call its LoadFromFile(). This will store the entire fill into the object. Then you can use the Strings[] property to get the line you want.
    :
    Thank you will try it
  • err0rerr0r Posts: 7Member
    didn't understand how to use TStringList, so i wrote little code:
    AsignFile...
    ...
    repeat
    var2:= var2+1;
    readln(file, var);
    until var1 = var2;
    ...
    that's how it was solved! =)

  • zibadianzibadian Posts: 6,349Member
    [b][red]This message was edited by zibadian at 2006-3-31 6:59:50[/red][/b][hr]
    : didn't understand how to use TStringList, so i wrote little code:
    : AsignFile...
    : ...
    : repeat
    : var2:= var2+1;
    : readln(file, var);
    : until var1 = var2;
    : ...
    : that's how it was solved! =)
    :
    :
    Here is a slightly better code, which also checks if the file is not empty:
    [code]
    while not eof(file) do
    begin
    var2 := var2+1
    readln(file, var);
    if var1 = var2 then Break;
    end;
    [/code]
    When using the TStringList variation:
    [code]
    var
    FileContents: TStringList;
    begin
    FileContents := TStringList.Create;
    FileContents.LoadFromFile('filename.txt');
    // Use Filecontents
    Line := FileContents[var2];
    // until you don't need them
    FileContents.Free;
    end;
    [/code]
  • err0rerr0r Posts: 7Member
    zibadian Thanks once again!
Sign In or Register to comment.