Link to home
Start Free TrialLog in
Avatar of mathes
mathes

asked on

I/O error 104 when printing a file

Hi experts,

with the following source code, I try to print an ASCII text file on my printer:

procedure TfrmPrintFile.Button3Click(Sender: TObject);

var filename: string;
    printer:textfile;
   
begin
  if opendialog1.execute then
   filename:=opendialog1.filename;
  assignPrn (printer);
  {$I-} rewrite (printer); {$I+}
  if ioresult<>0 then halt;
  while not eof (printer)
   do writeln (printer,filename);
  closefile(printer);
end;



Unfortunately Delphi complains about an  I/O error Nr. 104

in the line:

while not eof (printer)

What am I doing wrong here?

With kind regards

Christian

ASKER CERTIFIED SOLUTION
Avatar of Matvey
Matvey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mathes
mathes

ASKER

Dear Matvey,

I basically know how one can print a file. But I don't know my error in the posted source. So can you please review my code and stick please a little bit closer at it with your reply? (You did not explain to me the error 104). In this situation I don't need to set a specific font. A simple printout will perfectly meet my expectations. Moreover I don't know at program start what I want to print. This decision is made by the user during run time of the program. This is the reason why I have to open a file and send its content to the printer. In my specific situation I can't send a certain string to the printer- as you do it in your sample code- because the concrete content of the strings are not known at program start.

So please, try it again.

With kind regards

Christian



Hi. Your code produces the error on the "EOF(printer)" statement. It happends because the printer isn't concidered a readable device, so you can read it to find it's end. Therefore you have to avoid this statement.
To print your file you write something like this:

var filename, s: string;
    printer, t: textfile;
begin
  if opendialog1.execute then
    filename:=opendialog1.filename;
  try
    assignPrn(printer);
    rewrite(printer);
    reset(t, FileName);
    while not eof(t) do begin
      readln(t, s);
      writeln(printer, s);
    end;
  finally
    closefile(t);
    closefile(printer);
  end;
end;

-Sorry I was unclear and not specific.
All the best,
--Matvey
Avatar of mathes

ASKER

Dear Matvey,

thank you for reviewing my code concerning file printing. Now all works perfectly
as I like it.
Please excuse my unclear question. Of course it was well intended from
you when you provided me with details about font settings. I did not tell
you that I prefer a spartanic printout in this specific case.

With kind regards.

Christian