Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions


Posted on 1998-08-19
Last Modified: 2010-04-10
how do I print to the printer? do I use cout<< or fprintf, and can you show me how to use them, and do I have to declarethe printer I am using.  I am hook onto a network
Thanks fot your help!
Question by:lance100
  • 5
  • 5

Author Comment

ID: 1170693
Edited text of question
LVL 22

Accepted Solution

nietod earned 50 total points
ID: 1170694
In standard C++ (not dealing directly with a particular OS, like windows) you deal with a printer as if it is a file.  Thus you print by outputing to a file with the appropriate name.

LVL 22

Expert Comment

ID: 1170695
You need to open a file that cooresponds to a printer and then just write the information you would like to print.  There are lots of ways to open a file and write to it.  But the most convientient is likely to be using C++'s file streams.  Like

fstream Printer("LPT1",ios::out,filebuf::sh_none);

This opens a file called "LPT1".  The file is opened for output, but not input.   The file is opened without sharing (no one else can open it until you close it.)  

Then you can print to the file just like you ordinarily output to a stream.  Like

Printer << "This is a test" << endl;

You will ned to figure out the name of the file to open.  That depends on the operating system you are using, the network, and which printer you actually want.  If you can supply more details, I may be able to help you in that area.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 1170696
Thanks for the help.  I will try it now and let you now what happened

Author Comment

ID: 1170697
the help you gave me didn't work.  I am trying to print this from Visual C++ V5.  I have to print out "The estimate total..... is: #" # is a number that I get from a variable got m_total.
hers is what I did:
fstream Printer("LPT1",ios::out,filebuf::sh_none);
Printer << "This estimate total... is:" << endl;

do you have to indentify the printer or is it that you can't print it out because I am using visual C. I am using a HP laserjet 6p on win 95, which I is connected with Novell . Please tell me how to print out such a simple statement.
Thank You
LVL 22

Expert Comment

ID: 1170698
As I said before, the real trick is to figure out the name of the file to be opened.  Use "LPT1" if the printer is connected to LPT1.  Use "COM1" if the printer is connected to COM1, etc  For a network, printer, there is a file name associated with the printer,  But what it is will depend on your network's set-up.  I can't tell you what it is.  However, you will have to specify a path to the file.  Thus if the file server is FS1, and the file is Q1, the file and path you specify would be "\\FS1\Q1"  But remember that you need to put two \'s in a string in order to specify a single \, thus you would type "\\\\FS1\\Q1" in your source code.

Author Comment

ID: 1170699
can you tell me how to find the path to the printer.  I have tried going into the control panel and going into printers and I used the path it gave me but that did not work.
(\\\\ACS\\.hplaser6p.staff.acs) was what i used.  There was only \\ & \ so I added the extra slashes.
What did I do wrong or how do you fing the path?
LVL 22

Expert Comment

ID: 1170700
The slashes look right, but the file name might be wrong.  You've got three periods (".") in there which seems unlikely.  Are you sure that is name and path you saw in the printer set-up?

In my case, the printers seem to have to have names that follow the DOS 8.3 standard and there is no extension, so my file names are no more than 8 characters.  My paths are like

\\kepler\OkiLaser   => \\\\kepler\\OkiLaser
\\newton\IbmPro     => \\\\newton\\IbmPro

Author Comment

ID: 1170701
If my code looks like this for printing a little report, how do I get the
field width to stay the same ever time I print it out.
eg. when I type in a company field like Microsoft then I type one in like
Cape Breton Regional Municipality, the whole report gets messed up because
of the different lengths. And if there is no company name in the space all
the outputs jam together. How can I fix this problem to have a consistent

//////example of some of my variables declared above.
      m_custnum = _T("");
      m_ordernum = _T("");
      m_address = _T("");
      m_assigned = _T("");
      m_billcode = _T("");
      m_company = _T("");
      m_contact = _T("");
      m_contract = _T("");
      m_custponum = _T("");
      m_custref = _T("");

/////code to print out short report

fstream Printer("\\LPT1",ios::out,filebuf::sh_none);

      //output above strings

Printer<<"\rAlliance Computer Sytems / Atlantic Computer Services            SERVICE REPORT\n\r";
Printer<<"\r288 Welton Street   P.O. Box 1856     Phone 902 562-6600\n\r";
Printer<<"\rSydney, Nova Scotia B1P 6W4             Fax 902 562-6723\n\r\r";
Printer<<"\r _______________________________________________________________________________\n\r";
Printer<<"\r| Call Date: "<<m_calldate<<"  |  Status: "<<m_status<<"  |  Waiting for Parts: "<<m_waitparts<< " |  Order #: "<<m_ordernum<<"  |\n\r";      
Printer<<"\r| Customer #"<<m_custnum<<"                   | Customer Refernce #:"<<m_custref<<"|\n\r";
Printer<<"\r| Company: "<<m_company<<"           |      Customer P.O. #:"<<m_custponum<<"|\n\r";
Printer<<"\r| Address: "<<m_address<<"           |      Contract: "<<m_contract<<" |\n\r";
Printer<<"\r|                                              |\n\r";
Printer<<"\r|                                              |\n\r";              


      MessageBox("Your request is being Printed!");

LVL 22

Expert Comment

ID: 1170702
You should get a reference on the C++ iostreams.  If you are using VC there is stuff in the on-line help.  Otherwise you need to get a book that describes using streams.  There are lots.)

But briefly, you can set the width of the item to be output next with the setw() manipulator  (There are dozens of other manipulators, that is why you need a good reference)  Like

Printer<<" Order #: "<< setw(10) << m_ordernum << endl;

The setw(10) will pad the m_ordernum to 10 characters (with leading spaces), it doesn't print although it looks like it should.  Also note that the endl manipulators is used to indicate the end of the line.  The "\r\n" shouldn't ussually be used in C++.  endl should be used because it should be device/data independant.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question