Solved

Printing to PRN with Borland c++

Posted on 1998-05-31
14
675 Views
Last Modified: 2013-12-29
I am using Borland c++ for Windows v3.1 on Win95 and am trying to print to the printer attached to parallel port 1.  The "standard" stdprn file doesn't work, so I tried explicitly opening PRN:

p = fopen("PRN", "w");
fprintf(p, "this is a test\n");
fclose(p);

but the text is written to the screen!

Does anybody know what I'm doing wrong?
0
Comment
Question by:nipper
  • 5
  • 3
  • 3
  • +2
14 Comments
 

Author Comment

by:nipper
Comment Utility
Edited text of question
0
 
LVL 14

Expert Comment

by:smeebud
Comment Utility
Try setting up a generic/text printer fot prn to text, it's great.
==========
1. From a Dos Window, C:\>
In this exampe I'll use PRINT.PRN IN DIRECTORY 2 [C:\2]

YOU WOULD TYPE AT THE C:\>

C:\>PRINT C:\2\PRINT.PRN LPT1 [enter]

The lpt1 is my printer port. You would substitute LPT1 for you printer port.
It well may be the same.

 2. For making .prns to .txt, eliminating the prn altogeather..
Have you Windows 95 CD-ROM inserted.
If you are running Win95 (any version) go to start-settings-printers, click add printer
and add the printer Generic/Text Only (Manufacturer:Generic).
The next time you choose "Print to FILE" you can call the Filname.prn to Filname.txt.
Of course you do printer setup first and choose Generic/text Printer.
If you then "print" to this printer, it will create a text file.
Nice Huh!!

Files used by this printer Driver:
C:\WINDOWS\SYSTEM\TTY.DRV
C:\WINDOWS\SYSTEM\TTY.HLP
C:\WINDOWS\SYSTEM\UNIDRV.DLL
C:\WINDOWS\SYSTEM\UNIDRV.HLP
C:\WINDOWS\SYSTEM\ICONLIB.DLL

MORE OPTIONS CURTESY OF
Jon Eva  http://www.gallicrow.co.uk

1. From the Start menu, select Settings > Printers, the Printers window will
pop up, showing an icon for each of the printers you have at your disposal, plus
an icon called "Add Printer".
2. Double click on this icon.
3. The Add Printer Wizard window will pop up, click on Next.
4. Select the manufacturer and model of the printer you will eventually send the
.prn files to. Rather than scrolling through the entire list of manufacturers, just
press the first letter of their name (e.g. H for HP) on the keyboard
and the list will jump to the manufacturers starting with that letter.
5. Once the correct manufacturer / printer has been selected click on Next.
Keep the existing driver for this printer, click on Next.
You will be shown a list of ports, select the one which says:
"File: Creates a file on disk", click on Next.
6. Enter a name for this printer, e.g. "HP Laserjet (FILE)" and select whether to
use this printer as the default, click on Next.
Don't bother 'printing' a test page, click on Finish.
If you now 'print' to this printer a dialog asking you the name of the file to
print to will pop up.
=======
How can I print to the same file each time, without being prompted for a filename?

Short answer

Add a new local port named with a filename.
Long answer

Add a new printer that prints to the special "File:" port (see question 1).
From the printers' panel, right click on the printer you have just added
and select Properties.
A panel showing the properties for this printer will pop up, click on the
Details tab of this panel.
Click on the Add Port... button.
The Add Port panel will pop up, click on the Other radio button, and make
sure Local Port is highlighted, click on OK.
A small panel asking for the name of the port will pop up, enter the name of
a file in the edit box, for example C:\Output.prn.
Click on OK to dismiss the panel asking for the port name.
Click on OK to dismiss the printer properties panel.
If you now 'print' to this printer the output will be sent directly to
C:\Output.prn (or wherever), overwriting what was there before.
=========
How do I create a simple text file when "Printing to file"?
Short answer

Use the "Generic / Text" driver.
Long answer

From the Start menu, select Settings > Printers, the Printers window will
pop up, showing an icon for each of
the printers you have at your disposal, plus an icon called "Add Printer".
Double click on this icon.
The Add Printer Wizard window will pop up, click on Next.
Now select the "Generic / Text" printer driver - press the 'G' key, the list
of manufacturers should jump to
those beginning with G, highlight the one which says "Generic".
There should only be one model for the Generic manufacturer,
called "Generic / Text Only", make sure it is highlighted and click on Next.
Keep the existing driver for this printer, click on Next.
You will be shown a list of ports, select the one which says:
"File: Creates a file on disk", click on Next.
Enter a name for this printer, e.g. "Text File" and select whether to use
this printer as the default, click on Next.
Don't bother 'printing' a test page, click on Finish.
If you now 'print' to this printer a dialog asking you the name of the file
to print to will pop up.

Bud
0
 

Author Comment

by:nipper
Comment Utility
Actually, want I want to do is print to the default printer from a program written using Borlan c++ v3.1.  As I said, I tried the sample program (fragment) listed in the text of the original question, but the output goes to the screen.

I don't want to print a .prn file - I already know how to do that.  I am writing/have written a c-program using Borland c++ v3.1 (for Windows), but the fprintf to either "stdprn" or the FILE I have opened as "PRN" (or "LPT1") prints to the screen: no output on the printer.

Unless I have totally misunderstood your answer...

Any ideas?
0
 
LVL 14

Expert Comment

by:smeebud
Comment Utility
I think you did.
If your program allows you to print to file [prn] then.
What I'm proposing is you print to text.
Maybe I'm missunderstanding you??

If you do what to print to text, then
----------
 2. For making .prns to .txt, eliminating the prn altogeather..
Have you Windows 95 CD-ROM inserted.
If you are running Win95 (any version) go to start-settings-printers, click add printer
and add the printer Generic/Text Only (Manufacturer:Generic).
The next time you choose "Print to FILE" you can call the Filname.prn to Filname.txt.
Of course you do printer setup first and choose Generic/text Printer.
If you then "print" to this printer, it will create a text file.
Nice Huh!!

Files used by this printer Driver:
C:\WINDOWS\SYSTEM\TTY.DRV
C:\WINDOWS\SYSTEM\TTY.HLP
C:\WINDOWS\SYSTEM\UNIDRV.DLL
C:\WINDOWS\SYSTEM\UNIDRV.HLP
C:\WINDOWS\SYSTEM\ICONLIB.DLL

MORE OPTIONS CURTESY OF
Jon Eva  http://www.gallicrow.co.uk

1. From the Start menu, select Settings > Printers, the Printers window will
pop up, showing an icon for each of the printers you have at your disposal, plus
an icon called "Add Printer".
2. Double click on this icon.
3. The Add Printer Wizard window will pop up, click on Next.
4. Select the manufacturer and model of the printer you will eventually send the
.prn files to. Rather than scrolling through the entire list of manufacturers, just
press the first letter of their name (e.g. H for HP) on the keyboard
and the list will jump to the manufacturers starting with that letter.
5. Once the correct manufacturer / printer has been selected click on Next.
Keep the existing driver for this printer, click on Next.
You will be shown a list of ports, select the one which says:
"File: Creates a file on disk", click on Next.
6. Enter a name for this printer, e.g. "HP Laserjet (FILE)" and select whether to
use this printer as the default, click on Next.
Don't bother 'printing' a test page, click on Finish.
If you now 'print' to this printer a dialog asking you the name of the file to
print to will pop up.

Let me know if I'm not getting what you're after
Bud
0
 
LVL 1

Expert Comment

by:delirium
Comment Utility
Well, I don't know what exactly your program does, but wouldn't fprintf(stdprn,"text") work?
0
 

Author Comment

by:nipper
Comment Utility
Ok - we must be misunderstanding each other.

I have a c-program, written using Borland c++ v3.1.
Borland c/c++ allows the programmer to fprintf to the "standard" file "stdprn".  Just as "stdout" is the screen, "stdprn" is the default printer.  This program works in DOS - the fprintfs to "stdprn" are sent to the printer - but under Windows95 the output to stdprn is redirected to the screen...  The same occurs if an explicit fopen of "PRN" or "LPT1" is done.


0
 
LVL 7

Expert Comment

by:busuka
Comment Utility
I can tell you to use Int 17h, like:

void prnchar (char prch)
{
 _AH_=0; //
 _AL_=prch;
 _DX_=0; // 0 - LPT1, 1 - LPT2, etc.
 __int__(0x17);
}

or

asm
{
 xor ah,ah
 xor dx,dx
 mov al,prch
 int 17h
}

Then with simple loop you can write a function to print any string.

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Expert Comment

by:busuka
Comment Utility
Oops, error in registers syntax. I think you write _AX, _DX, etc.
0
 
LVL 1

Expert Comment

by:tchris
Comment Utility
Wouldn't this question be more appropriate in the C++ group?

Most of my experience has been C++ for DOS, but it seems that Windows isn't ever gonna let you go directly to the hardware that way.  You have to use some Win API calls to get through the print manager, etc..  Windows generally doesn't like / allow your programs to access hardware directly like you do in DOS.

Terry
0
 
LVL 7

Expert Comment

by:busuka
Comment Utility
But if your program is DOS-based and ran in DOS box under Win95
and uses direct access to h/w (like designed before all Windozes),
what will happen ? Win95 must emulate direct h/w access by
redirecting it to appropriate driver. Mea culpa, I gave C-based
answer, driving you to think that question belongs to one of C
forums. :-]
0
 
LVL 1

Expert Comment

by:tchris
Comment Utility
If he is trying to write a DOS program and run it in a DOS box, then BC++ for Win ain't gonna get it.  That makes Win .EXE's and I don't think they will do what you describe.  If its for Windows, it will require a Win API call, if it's for DOS it will require a DOS compiler.

Terry

0
 
LVL 7

Expert Comment

by:busuka
Comment Utility
Hint from nipper's comment:
This program works in DOS - the fprintfs to "stdprn" are
sent to the printer - but under Windows95 the output to stdprn is redirected to the screen.

Obviously EXE is DOS-executable.
0
 
LVL 1

Expert Comment

by:tchris
Comment Utility
Doh!  I missed that comment. :(

What's the skinny on your Win95 printer setup?  Just a regular Printer-"X"-attached-to-LPT1 setup?

Terry

0
 
LVL 7

Accepted Solution

by:
busuka earned 100 total points
Comment Utility
nipper, I believe that little piece of code, that I wrote will
help to solve your problem. Any feedback ?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A few months ago I had an issue with LaserJet 1020 printer which was installed to XP and Windows 7.  It was installed to XP and working, but when I tried to connect from a Windows 7 PC, it would attempt connection and then fail.  Sometimes the Spool…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now