Need trouble with repeat loop 
Author Message
 Need trouble with repeat loop

I am having trouble with a repeat loop.. I need to open a file and if it is
not there I have to make it so the user can designate another file to open
or quit the program... here is my code so far. Thank You

procedure open_file(var text_file : text);

var
source , error_ans : string;

begin
     repeat
          clrscr;
          write('Please enter the path to your text file and press ENTER:
');
          readln(source);
          assign(text_file, source);
          {$I-}
          reset(text_file);
          {$I+}

          if IoResult <> 0 then {if there is an error, ask if they want to
quit}
          begin
               writeln('File Could not be fond');
               writeln('Press Y to try another file or N to quit');
               read(error_ans);
               if (error_ans[1] = 'n') or (error_ans[1] = 'N') then
                  goodbye
          end;
     until (IoResult = 0)
end;

procedure output_text;

var text_file : text;
text_output : string;
counter : integer;

begin
     open_file(text_file);
     counter := 0;
     while not eof(text_file) do
     begin
          counter := counter + 1; {increase counter at each pass}
          readln(text_file, text_output);
          writeln(text_output);
          if counter mod 24 = 0 then
          pause_screen
     end
end;



Sun, 16 Nov 2003 13:56:20 GMT  
 Need trouble with repeat loop


Quote:
>I am having trouble with a repeat loop.. I need to open a file and if it is
>not there I have to make it so the user can designate another file to open
>or quit the program... here is my code so far. Thank You

You need to store the IORESULT in a temporary variable, otherwise its
value is changed by the other reads and writes.  Something like
(untested)
procedure open_file(var text_file : text);

var
source , error_ans : string;
TempIOResult:Integer;
begin
     repeat
          clrscr;
          write('Please enter the path to your text file and press
ENTER:');
          readln(source);
          assign(text_file, source);
          {$I-}
          reset(text_file);
          TempIOResult:=IOResult;
          {$I+}

          if TempIOResult <> 0 then {if there is an error, ask if they
want to quit}
          begin
               writeln('File Could not be fond');
               writeln('Press Y to try another file or N to quit');
               read(error_ans);
               if (error_ans[1] = 'n') or (error_ans[1] = 'N') then
                  goodbye
          end;
     until (TempIOResult = 0)
end;

procedure output_text;

var text_file : text;
text_output : string;
counter : integer;

begin
     open_file(text_file);
     counter := 0;
     while not eof(text_file) do
     begin
          counter := counter + 1; {increase counter at each pass}
          readln(text_file, text_output);
          writeln(text_output);
          if counter mod 24 = 0 then
          pause_screen
     end
end;

--
Martin Liddle, Tynemouth Computer Services, 27 Garforth Close,
Cramlington, Northumberland, England, NE23 6EW.
Phone: 01670-712624.  Fax: 01670-717324.
Web site: <http://www.tynecomp.co.uk>.



Sun, 16 Nov 2003 16:36:55 GMT  
 Need trouble with repeat loop
something like this might be what you want ?, i just slapped it together so
i doubt it works ehehe...

if you make var count a global varible you can use it outside of that
funtion...
have fun
adam.

program stuff;
uses crt,dos;

function f_exist(fn : string):boolean;
var f: text;
begin
  assign(f, fn);
  {$i-} reset(f); {$i+}
  if ioresult <> 0 then
  begin
    f_exist:=false;
    exit;
  end;
  close(f);
  f_exist:=true;
end;

function get_fname:string;
var s: string;
begin
    s:='';
    write('file: ');
    readln(s);
    if s <> 'exit' then if not f_exist(s) then s:='';
    get_fname:=s;
end;

function more:char;
var ox,oy : byte;
      ch : char;
begin
   ox:=wherex;
   oy:=wherey;
   write(' -- Press a key for more, or ESC to abort --');
   ch:=readkey;
   gotoxy(ox,oy);
   clreol;
   more:=ch;
end;

procedure display_f(fn: string);
var count : longint;
      s : string;
     f: text;
    ch : char;
begin
  assign(f, fn);
  {$i-} reset(f); {$i+}
  if ioresult <> 0 then
  begin
     writeln('Error - file could not be opened! - multitasking?' deleted it?
wtf???);
     halt(2);
  end;
  count:=0;
  ch:=#0;
  repeat
    inc(count);
    readln(f, s);
    writeln(count,'>',s);
    if count mod 24 = 0 then ch:=more;
  until (eof(f)) or (ch=#27);
  close(f);
  writeln(count,' Lines displayed');
end;

{'exit' quits}
var fn : string;
begin
  repeat
     fn:='';
     fn:=get_fname;
     if fn='' then writeln('Error - please specify a valid text file.');
  until (s='exit') or (s <> '');
  if s <> 'exit' then display_f(s);
end.

Quote:

>I am having trouble with a repeat loop.. I need to open a file and if it is
>not there I have to make it so the user can designate another file to open
>or quit the program... here is my code so far. Thank You

>procedure open_file(var text_file : text);

>var
>source , error_ans : string;

>begin
>     repeat
>          clrscr;
>          write('Please enter the path to your text file and press ENTER:
>');
>          readln(source);
>          assign(text_file, source);
>          {$I-}
>          reset(text_file);
>          {$I+}

>          if IoResult <> 0 then {if there is an error, ask if they want to
>quit}
>          begin
>               writeln('File Could not be fond');
>               writeln('Press Y to try another file or N to quit');
>               read(error_ans);
>               if (error_ans[1] = 'n') or (error_ans[1] = 'N') then
>                  goodbye
>          end;
>     until (IoResult = 0)
>end;

>procedure output_text;

>var text_file : text;
>text_output : string;
>counter : integer;

>begin
>     open_file(text_file);
>     counter := 0;
>     while not eof(text_file) do
>     begin
>          counter := counter + 1; {increase counter at each pass}
>          readln(text_file, text_output);
>          writeln(text_output);
>          if counter mod 24 = 0 then
>          pause_screen
>     end
>end;



Wed, 19 Nov 2003 22:41:03 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help on a simple repeat until loop

2. Help on a simple repeat until loop

3. More trouble with my file looping procedure:

4. need help with nested for loops

5. Need some simple help, with loop and files

6. I need help with loops and arrays!

7. I need help with loops and arrays!

8. Simple LOOP assistance needed

9. Need help: BLOB trouble

10. Help needed - trouble inserting new record

11. Need some help trouble shooting a network install

12. question: repeat-until

 

 
Powered by phpBB® Forum Software