Question

well, the following program opens a file and reads the first two strings that exist in the first line into two variables. The problem is that I’ve been trying to see what’s wrong with the following program, but I can’t see why it’s not working. The compiler compiles it without any problem, but when I run it, it says exited with exitcode 2. The problem is that the file exists. What seems to be the problem? Can anyone please help me? The code is the following one.

Program num;

Var
  f: Text;
  b, g: String;
  c: String[1];

Procedure thenum (a:String);
Begin
  Assign(f,'textfileoffile.txt');
  Reset(f);
  a := 'A';
  while not eof(f) and (a <> ' ') do
  begin
    Read (f,a);
  End;
  Writeln(a);
End;

Procedure sth ( j:String);
begin
  Assign(f,'textfileoffile.txt');
  Reset(f);
  j:='A';
  while not seekEoln and eof(f) do
  begin
    read(f,j);
  end;
  Writeln(j);
End;

begin
  Assign(f,'textfileoffile.txt');
  repeat
    Reset(f);
  until eof(f);
  thenum(b);
  read(f, c);
  sth (g);
  if eof(f) then
    Close(f);
  Readln;
End.
Was it helpful?

Solution

i think that the problem is, that you open same file a lot of times

repeat
Reset(f);
until eof(f);

and you open this file in your procedures, i think you must use f as a parameter of your functions and don't open it again, or make it global

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top