Delphi beginners

Moderators: netgert
Number of threads: 358
Number of posts: 982

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to read specific line from txt file in delphi? Posted by err0r on 31 Mar 2006 at 2:22 AM
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...
Report
Re: How to read specific line from txt file in delphi? Posted by zibadian on 31 Mar 2006 at 2:29 AM
: 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.
Report
Re: How to read specific line from txt file in delphi? Posted by err0r on 31 Mar 2006 at 2:55 AM
: : 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
Report
Re: How to read specific line from txt file in delphi? *SOLVED* Posted by err0r on 31 Mar 2006 at 6:49 AM
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! =)

Report
Re: How to read specific line from txt file in delphi? *SOLVED* Posted by zibadian on 31 Mar 2006 at 6:53 AM
This message was edited by zibadian at 2006-3-31 6:59:50

: 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:
  while not eof(file) do
  begin
    var2 := var2+1
    readln(file, var);
    if var1 = var2 then Break;
  end;

When using the TStringList variation:
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;

Report
Re: How to read specific line from txt file in delphi? *SOLVED* Posted by err0r on 31 Mar 2006 at 7:09 AM
zibadian Thanks once again!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.