Thread: printing stream in C++?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    printing stream in C++?

    i just realized that none of the books i have on c++ or the STL ever use a print stream. How do i access the printer in C++ or should i just use the old C method?

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435

    _ggs

    You used to be able to do

    prn << stuff;

    or at least I seem to remember being able to do that. Maybe it still works. Unfortunately you're going to have to know the control codes for your printer if you want anything but plain text.

    And if you're unlucky even plain text isn't guaranteed.
    .sect signature

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    nope, prn is undefined

    unless it's in it's own header or something (i'm including all the standards)

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    once again solved my own problem

    i've done this so many times.. heh

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

    its pretty easy to do

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    Unhappy Nope!

    i tested it and it actually doesn't work.. so i guess i'm still looking for an answer

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    >> printer.open("LPT1");

    should read: printer.open("lpt1");
    I belive it's case sensitive. Also, you'll need to write a page eject command. Watch your includes as well!

    hth,

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    could you post some code that works on your machine?

    Ted, could you (or someone else if you have it) post some code that would print hello world on the printer. In all the revisions of my code I got lost.

    my code looks like this now (but doesn't work)

    ...

    ofstream printer("lpt1");

    if(!printer)
    cout << "Error in printer";

    printer << "Name: " << firstName << lastName << endl;

    ...

    and I am including <iostream> and <fstream> along with some STL headers.

  8. #8
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227

    well..

    are you sure that your printer is at lpt1?

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    yeah

    after reinstalling dos drivers for my printer it did an autodetect and said printer connected at lpt1.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    This code works with my Okimate printer but not with my Canon printer. I don't know why! It also works with the networked printers at the college's computer lab and where I work.

    #include <iostream>
    #include <fstream> // ofstream
    #include <cstdlib> // exit


    using namespace std;


    int main (void)
    {
    char *line1 = "I am printing text to the printer!!!!!";

    const char EJECT = 0x0c; // printer page eject command
    ofstream print;
    print.open("LPT1");
    if( !print ){
    cerr << "File was not opened\n";
    exit(1);
    }

    cout << "Sending to lpt1\n";

    print << line1 << '\n';
    print << "Well, I am going to end printing now...\n";
    print << "Bye\n";

    print << EJECT;
    print.close();

    return 0;
    }

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    Exclamation i think that certain printer drivers respond to the code.

    my friend used my code (almost identical to yours) and it worked. my printer just doesn't do anything. I guess there's a mystery of C++. Alright well thanks for all your help.

    cprogramming.com Mystery #1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 02:30 AM
  3. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 05:15 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM