send output to a printer instead of the monitor.

Can anyone show me how to send output to a printer using C++?
You'll need to figure out how to rebind an ostream to your printer. How to do that will probably require some API stuff and knowledge on printer software relevant to the printer/drivers in question. It's going to be really specific to your printer so there's a lot of looking up you'll have to do.
The old way under Windows was to create a Printer Device Context and write to it instead of writing the Screen Device Context. Might be too low level for modern folks...ie, no need to get familiar with the actual printer itself, since Windows takes care of that for you.
Be wary of manipulating peripherals at low levels. The idea of streams is to wrap the complex low-level, possibly system-independent, user communications into a continuous interface for the user. Sometimes, using low-level code can become very specific to the peripherals/OS/compiler in question, and uses with any other stuff is unpredictable. That's why ideally, you could wrap a stream to your printer... however, this is not always possible. Which is why you might need low-level stuff.
do you want to do it on windows or unix?

Windows!
you can use istream_iterator such as
1
2
3
4
5
6
7
     istream_iterator<int> cin_it(cin);            // reads ints1 from cin
     istream_iterator<int> end_of_stream;  // end iterator value
                // writes Sales_items from the ofstream named outfile
               // each element is followed by a space
     ofstream outfile;
     ostream_iterator<Sales_item> output(outfile, " ");
He's trying to print to a printer, not a file. What does that have to do with it?
I am sorry !
I realize that I was blind
It's the following code:
1
2
3
4
5
6
7
8
      ostream_iterator<string> out_iter(cout, "\n");  // write one string per line to the standard output 
istream_iterator<string> in_iter(cin), eof;  // read strings from standard input and the end iterator
     // read until eof and write what was read to the standard output
     while (in_iter != eof)
         // write value of in_iter to standard output
         // and then increment the iterator to get the next value from cin
        *out_iter++ = *in_iter++;
Last edited on
I still really don't see what that code accomplishes with respect to the OP's objective. Seeing as almost any peripheral is outside standard C++ access, you'd need external API code to manipulate it, not just standard C++. Standard C++ makes no room for printer binding, afaik.
You can always redirect output to the printer from the shell.
program >LPT1 (PRN may also work)
This doesn't work well with some printers. Laser printers only print whole pages, for example. The best printers to do this are dot matrix printers fed with tractor-feed paper, followed by ink jets.
Last edited on
dot matrix printers fed with tractor-feed paper

Does the average person even own one of these anymore?
if you are using win32 api's then you can open the standard print dialog box and can print very easily.

http://msdn.microsoft.com/en-us/library/ms645524%28VS.85%29.aspx
Topic archived. No new replies allowed.