Thread: How to print to printer

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    19

    Question How to print to printer

    Hi!

    I've searched the tutorial section and the board but couldn't find my way around (niether the answer to my Q). So here goes..

    In my program I generate some messages to the user on the console. I'd like to print them out to the local printer. HOW?

    In VB there is a printer object to work with - is there one in VC++ too?

    A sample code would be nice and appreciated!
    Regards!
    Ales Zigon

    Rock 'em key things!!

  2. #2
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    A quick search on google with the items of: (print printer c++) revield a couple of sites that deal with printing. Here is a quote from one of the sites, Tips and tricks:
    Sending Output to the Printer
    How you print is now a function of your operating system. To print using Windows 95/98 or NT, you have to get your output to a print manager in the operating system.

    To direct output to a printer, you must first open a file stream to send output to. You can do this using the same method that you use to send output to a file. First, declare a variable of type fstream. Then open the stream using the member function, open. Instead of opening the stream using the name of a file, use the name of your printer port, for example, "LPT1."

    Once a file stream is open, you can print using the name of the stream variable followed by the << operator and the data you want to send to the printer. Be sure to close the stream when you no longer need to use it.

    The example program below will send text to a printer on port LPT1.

    #include <fstream.h>

    int main()

    {
    ofstream print; // stream variable declaration
    print.open("LPT1"); // open stream

    // Print Text (the character ‘\f’ will produce a form feed)
    print << "This text will print on the printer.\f";
    print.close(); // close stream
    return 0;
    }

    Important Note:
    Blindly directing text to a printer port assumes some things are true.

    It assumes that you have a printer attached to the specified port or that the port is being captured and redirected to a printer.
    It assumes that the printer is capable of accepting plain text. For example, a PostScript printer will not print unless the data comes through a PostScript printer driver. The code above does not make use of any drivers.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    To print to a local printer, it is very similiar to cout or cerr . The code is as follows:

    cprn << string1;

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    19
    Hi!

    Thanx both!

    I tried it and it works but only without special characters. That's OK for now.
    But, I assume that printing bold or aligning text is out of the question in this context (hope I'm wrong!)?


    Regards!
    Ales Zigon

    Rock 'em key things!!

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    I'm not an expert on this, but I don't think console programs deal with things as bold text. Aligned is probably possible though. You have to use stream manipulators (that is, if this works like cout <<). Don't remember the exact syntax for aligning text right now. I can look it up.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    19
    Originally posted by Hannwaas
    Don't remember the exact syntax for aligning text right now. I can look it up.
    Hi!

    Thanx! Please do! (i'd need it for printing).
    Regards!
    Ales Zigon

    Rock 'em key things!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Printer Prior To Loading Print Dialog Box
    By Beaner in forum Windows Programming
    Replies: 3
    Last Post: 10-10-2008, 01:02 PM
  2. print out to a printer, need help
    By IXxAlnxXI in forum C++ Programming
    Replies: 1
    Last Post: 03-25-2007, 01:24 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. send data to printer and print on paper
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 07-22-2002, 05:19 AM