26286 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Aha, I see you
switched on your
mobile Wi-Fi. YOU
FOOL!
What"s that Dell?
You"re out? HDS
punts pay-per-use
cloud storage
Spam and the
Byzantine Empire:
How Bitcoin REALLY
works
IBM gets ready to
push more UK and
Irish bods
overboard
Backup bods Veeam
quietly gobbling up
ever-greater market
share
LOHAN is GO!
Reg
spaceplane BLASTS
OFF on 14 September
Happy 23rd
birthday, Windows
3.0
Microsoft melds
SkyDrive Pro and
SharePoint
Yahoo! Oz! PAYS!
Punters! Pittance!
To! Search!
AMD"s three new
low-power chips
pose potent
challenge to Intel
Slashdot
Why We Should
Celebrate Snapchat
and Encourage
Ephemeral
Communication
Teens, Social
Media, and Privacy
Physicists Create
Quantum Link
Between Photons
That Don"t Exist At
the Same Time
Missile Test
Creates Huge
Expanding Halo of
Light Over Hawaii
3D Printers For
Peace Contest
Intel"s Linux
OpenGL Driver
Faster Than Apple"s
OS X Driver
Rough Roving:
Curiosity"s Wheels
Show Damage
Tesla Motors Repays
$465M Government
Loan 9 Years Early
Why the "Star Trek
Computer" Will Be
Open Source and
Apache Licensed
NYPD Detective
Accused of Hiring
Email Hackers
Article viewer

Printing in C++ w/streams



Written by:dimport
Published by:CodeX
Published on:2003-06-21 07:19:46
Topic:C++
Search OSI about C++.More articles by dimport.
 viewed 28700 times send this article printer friendly

Digg this!
    Rate this article :
Knowing how to print in C++ isnt something especially important but its nice to know and gives a decent footing into streams.

Be aware laser printers wont work with this program as inkjet/dotmatrix printers use the normal ascii character format whereas laser printers use some crazy ass format ive never worked with and im not sure itll work with USB printers either, but give it a whirl. I’ve seen this asked quite a few times on various forums and its one of the reasons i use iostream/cout over stdio/printf (although this can be done in the latter, its a pain in the ass) “how do i print from the console” the program itself is insanely short:

 

 
 #include
 
 
 
 int main()
 
 {
 
    int a;
 
    cout << "Initialising..." << endl;
 
    ofstream print;
 
    print.open("lpt1:", ios::out);
 
    cout << "Stream opened, sending text..." << endl;
 
    print << "Hello World" << endl;
 
    for(a = 0; a < 6; a++)
 
    {
 
       print << a << endl;
 
    }
 
    print << "f";
 
    print.close();
 
    return 0;
 
 }
 
 

 

 


If you’ve ever done file handling via streams this wont look so foreign, its pretty much the same, instead of opening the stream to a file on disk you open it to the printer port (assuming lpt1is your printer port). on the line:

 

 
 “ofstream print;”
 
 

 

 


You’re basically declaring a stream with the name ‘print’ the next line:

 

 
 “print.open("lpt1:", ios::out);”
 
 

 

 


Opens a stream to lpt1 the ios::out denotes the mode of transfer (in or out) and we’d have a job taking input from a printer, look at the line:


 

 
 “print << "Hello World" << endl;”
 
 

 

 


Note we use ‘print’ and not cout, this is because ‘print’ is the name of our stream and we can use that to send stuff to the printer just like you would use “cout” to send stuff to the screen as demonstrated in the for loop, the last two lines eject the paper from the printer and close the stream, easy enough.

This article was originally written by Pigsbig78

Did you like this article? There are hundreds more.

Comments:
Anonymous
2006-01-25 22:30:22
Nice - tutorial. I thought it was well written (although it was short) you still got your point across.
Anonymous
2009-02-11 17:00:31
whats the output ??? what will be printed on the printer??
CodeX
2009-02-11 19:28:41
Hello World
0
1
2
3
4
5
f
Anonymous
2009-04-22 14:35:09
I thought it was well written although it was short you still got your point across. flash games
Anonymous
2011-06-06 02:17:08
In this code, are the string charcters we write for printing treated according to ascii when communicating with the port?
I am sending commands to the port using hyperterminal and it is basically strings (treated as ascii).
To use C++ to communicate with the port, can I write it like: print<<"my command" << Will the port read it like ascii characters?

Thanks!
ObatAsamUrat
2011-06-16 07:16:51
hi there anyone, I became only checkin' out and about this site i like the muse informed, and still have not accomplish, if any individual would like to offer an engaging debate over it, remember to send me a email in skype, i'm michael duivis harry meautle
kaos distro
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
harry
Blog entry for Thu 28th Sep 12pm on Thu 28th Sep 12pm
Hi In school i want to net send my mates but hide who its coming off any ideas. no programs though as the machine in school sets off an alarm if it detects any batch files etc... thanks Harry


     
Your Ad Here
 
Copyright Open Source Institute, 2006