7

Can ofstream be used to write on a printer?

eg:

string nameOfPrinter = "xyz";
ofstream onPrinter(nameOfPrinter);
onPrinter << "Printing.... ";

If I do as above will I get the output by the printer (on the paper) ?

If not, why I won't get the output? Please suggest a way to print using a printer.

I am targeting the Windows platform (32bit)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

3 Answers3

4

If you happen to have your printer associated with LPT1 and a printer which support formfeeds.

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
    ofstream printer ("LPT1");
    if(!printer)
    {  return 1;
    }

    printer.puts("Test Test Test\n");
    printer.putc('\f');
    printer.close();
    return 0;
} 

LPT1 is also a file name in windows. But as known it is a reserved filename. So it will not be possible to have more than one file with the name LPT1. And this file is already managed by windows.

See for reserved filenames

fyr
  • 20,227
  • 7
  • 37
  • 53
3

Yes you can, Your code should be:

ofstream printer;
printer.open("lpt1");

I believe it's case sensitive(not sure "lpt1" or "LPT1"). Also, you'll need to write a page eject command.

EDIT:
LPT (Line Print Terminal) is name of the parallel port interface on IBM PC-compatible computers. Over the years, the parallel port interface has decreased use because of the rise of Universal Serial Bus.

In DOS, the parallel ports could be accessed directly on the command line. For example, the command type c:\autoexec.bat > LPT1 would direct the contents of the autoexec.bat file to the printer port(recognized by the reserved word LPT1"). A PRN device was also available as an alias for LPT1.

Microsoft Windows still refers to the ports in this manner in many cases, though this is often fairly hidden.

In the Linux operating system the first LPT port is available via the filesystem as /dev/lp0.

To write to a printer, one should simply open the printer as if it were a file (the printer name is system-dependent; on Windows machines, it will be lpt1 or prn, while on unix machines, it will be something like /dev/lp), then write whatever text has to be written.

Sample program could be as simple as:

std::ofstream print; 
print.open("LPT1");
if (!print)
    return 0;
print << data;
print.close();
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

How would the file stream know the difference between the name of a printer and a file that just happened to share the name of a printer? So no; you can't print to a printer by specifying the name of a printer.

Printing in Win32 is not a trivial task. You can't simply shove some characters at a printer; it needs to know about page layout, fonts, etc. Basically, the way to do it from Win32 is to "draw" to the printer with GDI commands. Beginner-level info can be found here.


Correction: apparently, you can stream output to the printer with a stream. However, it requires that the user has enabled some legacy functionality, so it isn't necessarily always available.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • [Printing reference on MSDN](http://msdn.microsoft.com/en-us/library/ff686805%28v=VS.85%29.aspx). – Cat Plus Plus Jul 01 '11 at 09:23
  • -1 ... you cant have certain filenames twice in windows. See the reference to reserved filenames in my answer. – fyr Jul 01 '11 at 09:27
  • @fyr: Fair enough. But it's still not a good way to write to a file, since as you point out, it isn't guaranteed to actually work. – Nicol Bolas Jul 01 '11 at 09:58