Solved

Printing a text file

Posted on 1998-07-05
8
353 Views
Last Modified: 2012-05-04
Hi I have to do a project in pascal 7 my project is finished
and it saves a report to a text file, now I would like to write a procedure that will test if the printer is on, on-line, has paper in etc and if all these are true, it must print the text file. I need help urgently!! Please.
0
Comment
Question by:Gurkin
8 Comments
 
LVL 1

Accepted Solution

by:
FuzzyLogic earned 50 total points
ID: 1217150
You can use the "printer" unit of TP.
Just add the following line:
  uses Printer;

And you can send whatever you want to the printer by using the file handler "lst":
  Writeln(lst,'Hello world!!!');

Fuzzy
0
 

Author Comment

by:Gurkin
ID: 1217151
FuzzyLogic, thank you, I need to be able to print on WINDOWS95 and MSDOS based printers..... cna you help there??
0
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1217152
I don't know how Win95 handle the printer (I haven't install that bloware on my PC), but I think it will work too under Win95. You just have to check.
And another thing: To check if the printer is OK, you have to call int 17h, service AH=02. You'll get the printer status in the AH:
  Bit0 - TimeOut              Bit5 - Out of paper
  Bit3 - I/O Error            Bit6 - Acknowledge
  Bit4 - Selected             Bit7 - Not Busy

You can call the interrupt using asm statement or use a dos-unit procedure (I don't recall right now what its name, because I don't use it).
Fuzzy
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:saar2
ID: 1217153
Another solution for you is:

Var
 F:Text;

Begin
 Assign (F,'LPT1');
 Rewrite (F);
 Write (F,'Hello World');
 Write (F,#12) {means end of page}
 Close(F);
End.
0
 

Expert Comment

by:frits
ID: 1217154
Try this unit.
I wasn't able to test it, but it should work.

---------- knip knip -------------------------

UNIT Lister;

INTERFACE

CONST
   prSkipLine  = 1;
   prFormFeed  = 2;
   prBlindMode = 4;

TYPE
   WorkString = string[126];

PROCEDURE PrintLn(PrintStr: WorkString);
FUNCTION CheckOnline : Boolean;

IMPLEMENTATION
USES Crt,Printer;
CONST
  PageWidth   = 80;
  PrintLength = 55;
  PathLength  = 65;

VAR
  CurRow : integer;
  FormFeed:CHAR;
  VerticalTabLength:INTEGER;
  BlindMode:BOOLEAN;

Function CheckOnline : Boolean; Assembler;
ASM
   MOV      AH, 02H     {Int 17,  Service 2 for printer status}
   MOV      AL, 00H     {just playing it safe, probably not needed}
   MOV      DX, 00H     {printer 0}
   INT      17H
   MOV      AL, 00H     {set checkonline false}
   TEST     AH, 10H     {if bit 4 is set z flag=0, if not z flag=1}
   JZ       @No         {leave function false if z flag is set}
   INC      AL
   @No:
END;

Procedure UnderScOn;
   begin
      write(lst,#27#45#1);
   end;

Procedure UnderScOff;
   begin
      write(lst,#27#45#0);
   end;

PROCEDURE VerticalTab;
  VAR i: integer;
  BEGIN
    for i := 1 to VerticalTabLength do Writeln(LST)
  END;

PROCEDURE PrintLn(PrintStr: WorkString);
  BEGIN
    CurRow := Succ(CurRow);
    if Length(PrintStr) > PageWidth then Inc(CurRow);
    if CurRow > PrintLength then
    BEGIN
      Write(LST,FormFeed);
      VerticalTab;
      CurRow := 1;
    END;
    IF PrintStr[1] IN ['!','@'] THEN BEGIN
      UnderScOn;
      WriteLn(LST,COPY(PrintStr,2,LENGTH(PrintStr)-1));
       UnderScOff
    END
    ELSE Writeln(LST,PrintStr)
  END;

END.

-------------------- knip knip -----------------------------

PROGRAM TestLister;

USES Crt,Lister;

BEGIN
   IF CheckOnLine THEN
      PrintLn('Hello world, I`m a printed line.....')
   ELSE
      WriteLn('Printer is off-line....');
   IF ReadKey=#27 THEN HALT
END.

0
 

Author Comment

by:Gurkin
ID: 1217155
Thank you all for the help on printing, It was a real help....
0
 

Expert Comment

by:degraaf
ID: 1217156
You could also use the directives {$I-} and {$I+} to check the outcome of any print instruction. IOResult is set by using this (after reading its value it is reset). A complete list of IO values can be found in the Pascal help file.

For example:

 Assign (F,'LPT1');
{$I-}
 Rewrite (F);
{$I+}
io:=IOResult;
if io<>0 then
{your error handling, depending on error code}

0
 
LVL 7

Expert Comment

by:Motaz
ID: 1217157
(***** This is a very simle code to use a printer to print  *****)
(***** a text file         ***)

uses Printer;

(*********** This function for test if printer ready ************)

Function TestPrinter:boolean;
begin
 {$i-}
  Write(Lst,#7);
 {$i+}
 if IOResult=0 then
   TestPrinter:=True
else
   TestPrinter:=False;
end;

(******** This procedure print a text file send to it ********)

procedure PrintFile(FileName:String);
var
      F:Text;
      St:String[100];
begin
   Assign(F,FileName);
  Reset(F);     (*** Open text file ***)
 
 while Not Eof(F) do  
  begin
    Readln(F,St);     (*** Read line from file ***)
    Writeln(Lst,St);   (*** Write this line into printer ***)
  end;
  Close(F);
  Write(Lst,#12);     (*** Eject the paper ***)
end;

begin
  if TestPrinter then
     PrintFile('Test.txt');
end.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question