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?
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?
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
unless it's in it's own header or something (i'm including all the standards)
i've done this so many times.. heh
ofstream printer;
printer.open("LPT1");
its pretty easy to do
i tested it and it actually doesn't work.. so i guess i'm still looking for an answer
>> 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,
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.
are you sure that your printer is at lpt1?
after reinstalling dos drivers for my printer it did an autodetect and said printer connected at lpt1.
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;
}
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