Howdy, Stranger!

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

Categories

How to read specific line from txt file in delphi?

err0rerr0r Member Posts: 7
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 Member Posts: 6,349
    : 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 Member Posts: 7
    : : 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 Member Posts: 7
    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 Member Posts: 6,349
    [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 Member Posts: 7
    zibadian Thanks once again!
Sign In or Register to comment.