Can be used for printing on a printer.

Can ofstream be used to write to the printer?

eg:

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

If I do as described above, will I get output on a printer (on paper)?

If not, why don't I get a way out? Please suggest a method of printing using a printer.

I focus on the Windows platform (32 bit)

+7
source share
3 answers

If you have your printer associated with LPT1 and a printer that supports forms.

 #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 the file name in the windows. But, as you know, this is a reserved file name. Thus, it will not be possible to have more than one file named LPT1. And this file is already managed by windows.

See reserved file names

+4
source

Yes you can, your code should be:

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

I find it case sensitive (not sure about “lpt1” or “LPT1”). In addition, you need to write a page retrieval command.

EDIT:
LPT (Line Print Terminal) is the name of the parallel port interface on IBM PC compatible computers. Over the years, the parallel port interface has been decreasing due to the growth of the universal serial bus.

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

Microsoft Windows still treats ports this way in many cases, although this is often quite hidden.

On Linux first LPT port is accessible via the file system as /dev/lp0 .

To write to the printer, you just need to open the printer as if it were a file (the name of the printer depends on the system; on Windows machines it will be lpt1 or PRN , and on Unix machines it will be something like /dev/lp ), then write what text should be written.

An example program might be simple:

 std::ofstream print; print.open("LPT1"); if (!print) return 0; print << data; print.close(); 
+3
source

How would the file stream know the difference between the name of the printer and the file that just passed the name of the printer? Then no; You cannot print to a printer by specifying a printer name.

Printing in Win32 is not a trivial task. You cannot just drag and drop some characters onto the printer; he should know about page layout, fonts, etc. Basically, the way to do this from Win32 is to "draw" to the printer using GDI commands. Entry level information can be found here .


Correction: apparently, you can transfer the stream to the printer with the stream. However, this requires the user to enable some deprecated features, so it is not always available.

+2
source

All Articles