views:

260

answers:

4

I am using VC++ 2008 express edition for C. When I try to run this:

/* Demonstrates printer output. */

#include <stdio.h>

main()
{ 
 float f = 2.0134;

 fprintf(stdprn, "This message is printed.\n\n");
 fprintf(stdprn, "And now some numbers:\n\n");
 fprintf(stdprn, "The square of %f is %f.", f, f*f);

 /* Send a form feed */

 fprintf(stdprn, "\f");
}

I get four of these errors: error C2065: 'stdprn' : undeclared identifier.

On this forum, they wrote that it works to define the printer as follows:

FILE *printer;
printer = fopen("PRN", "w");

EDIT It builds with a warning that fopen is unsafe. When it runs the error appears:

Debug Assertion fails. File: f:\dd\vctools\crt_bld\self_x86\crt\src\fprintf.c Line: 55

Expression: (str != NULL)

+1  A: 

The stdprn stream was an extension provided by Borland compilers - as far as I know, MS have never supported it. Regarding the use of fopen to open the printer device, I don't think this will work with any recent versions of Windows, but a couple of things to try:

  • use PRN: as the name instead of PRN (note the colon)
  • try opening the specific device using (for example) LPT1: (once again, note the colon). This will of course not work if you don't have a printer attached.
  • don't depend on a printer dialog coming up - you are not really using the WIndows printing system when you take this approach (and so it probably won't solve your problem, but is worth a try).
anon
+1  A: 

I do not have a printer attached, but I do have the Microsoft XPS document writer installed, s it shoulod at least bring up the standard Windows Print dialog from which one can choose the printer.

No. It wouldn't bring up a dialogue. This is because you are flushing data out to a file. And not going through the circuitous Win32 API.

The print doesn't work because the data is not proper PDL -- something that the printer could understand. For the print to work fine, you need to push in a PDL file, with language specific constructs. This varies from printer to printer, a PS printer will need you to push in a PostScript snippet, a PCL -- a PCL command-set and in case of MXDW you will have to write up XML based page description markup and create a zip file (with all resources embedded in it) i.e. an XPS file to get proper printout.

The PDL constructs are important because otherwise the printer doesn't know where to put the data, which color to print it on, what orientation to use, how many copies to print and so on and so forth.

Edit: I am curious why you are doing this. I understand portability is probably something you are trying to address. But apart from that, I'd like to know, there may be better alternatives available. Win32 Print Subsytem APIs are something that you ought to lookup if you are trying to print programmatically on Windows with any degree of fidelity.

Edit#2:

EDIT It builds with a warning that fopen is unsafe.

This is because MS suggests you use the safer versions nowadays fopen_s . See Security Enhancements in the CRT.

When it runs the error appears:

Debug Assertion fails. File: f:\dd\vctools\crt_bld\self_x86\crt\src\fprintf.c Line: 55 Expression: (str != NULL)

This is because fopen (whose return value you do not check) returns a NULL pointer. The file open failed. Also, if it did succeed a matching fclose call is called for.

dirkgently
A: 

There's no such thing as stdprn in ANSI C, it was a nonstandard extension provided by some compilers many years ago. Today to print you have to use the specific APIs provided on your platform; to print on Windows you have to use the printing APIs to manage the printing of the document and obtain a DC to the printer and the GDI APIs to perform the actual drawing on the DC.

On UNIX-like OSes, instead, usually CUPS is used.

Matteo Italia
A: 
tommieb75