## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

Choose which line "to readln" in text file Posted by blackr1234 on 6 Feb 2011 at 10:14 AM
May I choose which line to read (readln) in a text file?

If yes, then what is the command?
If no, is there any way better than the following?

VAR i, which_line: integer;
s: STRING;
text_file: text;

BEGIN

assign(text_file, 'C:\text_file.txt');
FOR i:=1 TO which_line DO readln(text_file, line);


Re: Choose which line "to readln" in text file Posted by _Atex_ on 6 Feb 2011 at 11:48 PM
: May I choose which line to read (readln) in a text file?
:
: If yes, then what is the command?
: If no, is there any way better than the following?
:

:
If you work with text files then you can only read line by line ( or value by value) so to skip to a particular line you must read through the whole file until you reach the line or value you're seeking. If you open a text file as an untyped one (i.e var f:file; ) then you can use seek to jump to every position at any time and read from there. That's easy if you have an even line length text, but quite complicated for irregular line lengths...
Re: Choose which line "to readln" in text file Posted by blackr1234 on 7 Feb 2011 at 3:06 AM
Thanks for the suggestion of using FILE variable and the SEEK function.
I have a little problem with my another program.

PROBLEM:
Let me explain.
During name creation, if I enter <3 letters, program closes.

PROGRAM test_1;

USES crt;

VAR IOcode: integer;
name, name2: STRING;
error: boolean;
save: text;

BEGIN

assign(save, 'C:\save.txt');

REPEAT
{$I-} reset(save); {$I+}
IOcode:= IOresult;
IF IOcode=2 THEN writeln('Create') ELSE IF IOcode=0 THEN writeln('Login');

IF length(name)>=3 THEN BEGIN

error:= FALSE;

IF IOcode=2 THEN BEGIN rewrite(save); writeln(save, name); writeln('Done!!!'); delay(500) END ELSE
IF IOcode=0 THEN BEGIN readln(save, name2) END

END ELSE

IF length(name)<3 THEN BEGIN error:= TRUE; writeln('>=3 letters please!'); delay(500) END;

close(save); clrscr

UNTIL (name=name2) AND NOT error

END.

Here's a screenshot (not exactly the same as the one above):

I find out that the error code for File Not Open is 103.
My informal solution is to change the IF condition from IOcode=2 to IOcode<>0, also place close(save); between {$I-} {$I-}.

When I create a new name, the program flow is THE SAME as the following;
assign(save, 'C:\save.txt');
{$I-} reset(save); {$I+}          {IOresult=2}
close(save);


But I don't know why the exe gives me error code 103 instead of 2.
Re: Choose which line "to readln" in text file Posted by blackr1234 on 7 Feb 2011 at 12:46 PM
Problem solved, I finally simplify the program and get the following conclusion:
assign(save, 'C:\save.txt');
{$I-} reset(save); {$I+}     writeln(IOresult);
{$I-} close(save); {$I+}     writeln(IOresult);


Output:
2
103


2 stands for File not found. 103 stands for File not open.

To conclude, I need not to close a text file if RESET() results in an error. Otherwise, another error occurs.

## 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