Go Premium for a chance to win a PS4. Enter to Win



Posted on 1998-08-19
Medium Priority
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 200 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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

783 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