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.

Choose which line "to readln" in text file

blackr1234blackr1234 Posts: 31Member
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?

[code]
VAR i, which_line: integer;
s: STRING;
text_file: text;

BEGIN
write('Line to read:'); readln(which_line);

assign(text_file, 'C: ext_file.txt');
FOR i:=1 TO which_line DO readln(text_file, line);
[/code]

Comments

  • _Atex__Atex_ Posts: 163Member
    : 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 [b]seek[/b] 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...
  • blackr1234blackr1234 Posts: 31Member
    Thanks for the suggestion of using FILE variable and the SEEK function.
    I have a little problem with my another program.


    [u][b]PROBLEM:[/b][/u]
    Let me explain.
    During name creation, if I enter <3 letters, program closes.
    No error in login process.



    [code]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');

    write('Name: '); readln(name);

    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.[/code]

    Here's a screenshot (not exactly the same as the one above):
    [img=http://img141.imageshack.us/img141/9229/asdkh.png]



    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;
    [code]
    assign(save, 'C:save.txt');
    {$I-} reset(save); {$I+} {IOresult=2}
    close(save);
    [/code]
    But I don't know why the exe gives me error code 103 instead of 2.
  • blackr1234blackr1234 Posts: 31Member
    Problem solved, I finally simplify the program and get the following conclusion:
    [code]
    assign(save, 'C:save.txt');
    {$I-} reset(save); {$I+} writeln(IOresult);
    {$I-} close(save); {$I+} writeln(IOresult);
    [/code]

    Output:
    [code]
    2
    103
    [/code]

    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.
Sign In or Register to comment.