Recent

Author Topic: Runtime Error 2? Linux?  (Read 18321 times)

MuteClown

  • New Member
  • *
  • Posts: 33
Runtime Error 2? Linux?
« on: February 24, 2010, 11:29:55 pm »
I am having some problems, i keep getting Runtime error 2. I think it means it cant find a file, all im doing is following a tutorial on about reading/writing to a file. I first couldn't get my code to work so i copied the example and changed the it for my txt file yet still doesnt work. I am using Linux and wondering if there is some permissions i need it to be granted. I am mainly used a text editor but i also tried it on Lazarus and it just closed and said it had an error.

This is the example i took from the tutorial, mute.txt and mute2.txt are in the same directory.

Code: [Select]
Program pt9;
uses crt;
var
   mychar : char;
   filein, fileout : text;

begin
   assign (filein, 'mute.txt');
   reset (filein);
   assign (fileout, 'mute2.txt');
   rewrite (fileout);
   read (filein, mychar);
   write (fileout, mychar);
   close(filein);
   close(fileout)
end.

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Runtime Error 2? Linux?
« Reply #1 on: February 25, 2010, 12:08:18 am »
assignfile(), closefile()
Lazarus/FPC on Linux

MuteClown

  • New Member
  • *
  • Posts: 33
Re: Runtime Error 2? Linux?
« Reply #2 on: February 25, 2010, 12:28:39 am »
assignfile(), closefile()

Thank you for your reply, those aren't identified.

MuteClown

  • New Member
  • *
  • Posts: 33
Re: Runtime Error 2? Linux?
« Reply #3 on: February 25, 2010, 09:16:11 am »
Well i know the issue is at the Assign, that's where the program stops.
I really need some help here, i've been searching all the documentary and online guides for a solution.
Even tried using FileOpen etc.. but i still need to assign it. the file is there same directory, tried putting the whole address in as well no use. I have literally spent hours searching for a solution, its a simple thing yet it wont work.  :o 

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Runtime Error 2? Linux?
« Reply #4 on: February 25, 2010, 10:01:50 am »
This code works here. Check the file permissions and try absolute paths.

Bart

  • Hero Member
  • *****
  • Posts: 5647
    • Bart en Mariska's Webstek
Re: Runtime Error 2? Linux?
« Reply #5 on: February 25, 2010, 04:14:07 pm »
Do some debugginng to see where it fails;

Code: [Select]
uses sysutils;

var
  filein, fileout: textfile;
  mychar: char;

begin
  {$I-}
   if not fileexists('mute.txt') then begin writeln('mute.txt is not there'); halt; end;
   assign (filein, 'mute.txt');
   reset (filein);
   if IOResult <> 0 then begin writeln('IO Error on Reset mute.txt'); halt; end;
   if not fileexists('mute2.txt') then begin writeln('mute2.txt is not there'); halt; end;
   assign (fileout, 'mute2.txt');
   rewrite (fileout);
   if IOResult <> 0 then begin writeln('IO Error on Rewrite mute2.txt'); halt; end;
   read (filein, mychar);
   if IOResult <> 0 then begin writeln('IO Error on Read from mute.txt'); halt; end;
   write (fileout, mychar);
   if IOResult <> 0 then begin writeln('IO Error on Write to mute2.txt'); halt; end;
   close(filein);
   if IOResult <> 0 then begin writeln('IO Error on Close mute2.txt'); halt; end;
   close(fileout);
   if IOResult <> 0 then begin writeln('IO Error on Close mute2.txt'); halt; end;
end.

Bit over the top, but it should halt at the first IO Error.

Strange behaviour though when both mute.txt and mute2.txt are zero byte files.
It runs without error, but shouldn't it set InOutRes to some non-zero value, because the read(filein,mychar) should fail???
(I know it can be prevented by  first checking Eof(filein) )
It then writes a ^Z (on linux!) to mute2.txt.
Tested on linux with fpc 2.2.4.

Bart

MuteClown

  • New Member
  • *
  • Posts: 33
Re: Runtime Error 2? Linux?
« Reply #6 on: February 25, 2010, 05:50:32 pm »
half way through writing my response i tried it just from a executing it form terminal and it worked that way, just wouldn't work in Lazarus.
dunno why.

 

TinyPortal © 2005-2018