• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:


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!
  • 5
  • 5
1 Solution
lance100Author Commented:
Edited text of question
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.

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.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

lance100Author Commented:
Thanks for the help.  I will try it now and let you now what happened
lance100Author Commented:
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
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.
lance100Author Commented:
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?
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
lance100Author Commented:
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!");

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now