Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

Printing a text file

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
Gurkin
Asked:
Gurkin
1 Solution
 
FuzzyLogicCommented:
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
 
GurkinAuthor Commented:
FuzzyLogic, thank you, I need to be able to print on WINDOWS95 and MSDOS based printers..... cna you help there??
0
 
FuzzyLogicCommented:
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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
saar2Commented:
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
 
fritsCommented:
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
 
GurkinAuthor Commented:
Thank you all for the help on printing, It was a real help....
0
 
degraafCommented:
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
 
MotazCommented:
(***** 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

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now