Printing to a network printer from the screen

I am writing a program and need to give to person running the program the option to print to the network printer.
nate222Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
What network? what OS?  

From a win32 console application you can simply open the network printer as a file, like

fstream Printer("\\\\servername\\printername",ios::out);

and then print information to the file, like

Printer << "some text" << endl;

A higher teck way would be print through the windows API, but I suspect that is not what you want, right?
0
nate222Author Commented:
c++ runs this program in ms-dos so what I need it to print the "screen" by giving the user the option to press a button that will automatically send the stuff on the screen to the printer. (since we are in school, the printers are networked, so I have to work around that problem.
 sorry if it don't sound like I don't know what I am talking about 'cause I don't.
0
nietodCommented:
What compiler are you using.  I'm asking because I suspect that you are writting a windows console program, which is a windows program, not a DOS program (But looks like a DOS program.)

When you say

>> user the option to press a button

Do you mean a graphical button?  or a keyboard button?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

rdfCommented:
If you have the user press ALT-PrintScreen, I think the current active screen will be copied to the clipboard, then you can paste the clipboard onto MS paint or word and print from there
 just an idea
0
nietodCommented:
rdf, This can all be done directly from his program, but I'm not yet sure fo the details of what he wants to do.
0
nietodCommented:
rdf, This can all be done directly from his program, but I'm not yet sure fo the details of what he wants to do.
0
nate222Author Commented:
OK! lets make this a little easer. All i want to do is, as soon as the "answer" is printed to the screen...have it also sent to the printer.
 
0
nietodCommented:
that's not necessarily easier.  This isn't likely to be hard.  The problem is I can see about 100 different posibliities here and I'm not sure what is the actual one you want.

Can you describe in detail what you want?  Like what is this "answer", is it what the program prrints to the screen?  is it what the user types?  etc.  there are a lot of details I need to know to decide on the final design.  

I also need to know if it is for a DOS program or a win32 console.  Thre is a big difference.  Do you know?  Do you know what compiler you are using?
0
mflamCommented:
Nate, did you try nietod's first answer?
              --------------------------
What mistake did you get?
Or did it just not work?

Moshe
0
nietodCommented:
I really, haven't given the answer yet.  That's sort of the "core" of th answer, but not the details.  I'm trying to narrow down the posibilities so I can give one nice answer, instead of 10 answers.

nate, I just need a litle more information from you, then I can give you some good code and a nice complete answer.
0
nate222Author Commented:
I want to print a set of variables to a local and/or network printer. These variables have been predefined by the program. This must be accomplished within the program without using notepad or anthying like that. The user will type print when they want it to print. You were right, it is a win32 console program.
0
nate222Author Commented:
It is borland c++
0
nietodCommented:
Okay, now that doesn't seem to have anythign to do with the screen.  you has said

>>  I need it to print the "screen"

which is why I was asking a lot of those questions.  (to find out how best too obtain the information currently on the screen....)

If you just need to print information you "already have", you should just do what I suggested at the start.  pen the printer as a file and write to the file.  Like

fstream Printer("\\\\servername\\printername",ios::out);

string TextString = "This is a line to be printed;
int X = 5;

Printer << "some text" << endl;
Printer << TextString << endl;
Printer << X;
Printer << '\f';

The last line there prints a form feed character  ('\f').  This is needed for "page printers", like laser and ink jet printers.  They biuffer a page of text until they get the form feed character, then they print the page.

Does that help?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nate222Author Commented:
This is what I needed, but it still don't print to the network printer...any ideas why???
0
nietodCommented:
What exactly are you doing?  can you post the code?
0
nate222Author Commented:
#include<iostream.h>
#include<fstream.h>

int main()
{
int answer;
int number;

fstream Printer("\\E408-STARPOINT-CSD\.F20-DJ855-ROW3",ios::out);

cout<<"Enter a number: ";
cin>>number;
cout<<endl;
answer = number * 2;
cout<<"The answer is: ";
cout<<answer;

Printer<<answer<<endl;
Printer << '\f';

int delay;
cin>>delay;
return 0;
}

//make this print please!!!

0
nietodCommented:
One problem (maybe the only problem) is that in C++, inside a "string literal" the "\" character is an "escape" character.   It is not inserted in the string, it is used to insert a special character into the string based on the character that follows the "\".  So the "\n" inserts a newling (line feed) character, for example.  If you want to put a "\" in the string, you must use two \s in a row.  

So in your string, where you need a single "\", you should have two like "\\".  if you need two \s, you should have four, like "\\\\"  So try

fstream Printer("\\\\E408-STARPOINT-CSD\\.F20-DJ855-ROW3",ios::out);
0
nate222Author Commented:
      ////////////////////////////////////////////
   //////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///////This is saving it not printing it///////
////////////////////////////////////////////////////////////
   /////////////////////////////////////////////////////
     //////////////////////////////////////////////

#include<iostream.h>
#include<fstream.h>

int main()
{
int answer;
int number;

fstream Printer("\\\\E408-STARPOINT-CSD\\.F20-DJ855-ROW3",ios::out);

cout<<"Enter a number: ";
cin>>number;
cout<<endl;
answer = number * 2;
cout<<"The answer is: ";
cout<<answer;

Printer<<answer<<endl;
Printer << '\f';

int delay;
cin>>delay;
return 0;
}
0
nietodCommented:
I'm a little confussed
Are you saying this is getting saved to a file, instead of printed?

That just means you specified the wrong file name.  the file name should have two "\"'s (but you have to specify 4 to get 2...) followed by the server name of the server that has the printer.  followed by a "\" (but you need to specify two), followed by the name that the printer is shared under.  (You can get this name by right clicking on the printer in windows.)
0
nietodCommented:
To find out the printer's name you hove to right click on it and then select "sharing" from the pop-up menu.  The shared name will be listed in the dialog box that comes up.

I'm leaving town for about 5 days in about 24 hours.  I won't be able to respond while I'm gone.
0
nate222Author Commented:
OK. have the 4/ instead of 2 and the 2/ instead of 1 but it still saves it to the file. I am sure that is the right file and server name because someone in my class has it to work, but will not help me anymore than giving me the file/server information.    
                                 HAVE FUN ON YOUR TRIP
0
nietodCommented:
it has to be the wrong name.  If you can read a file with that name, then it can't be the right name, because a file with the right name would be write-only file to the printer.  i.e. the "right" name will be treated differently.  If the name isn't treated differently, its not the right name.
0
nate222Author Commented:
////////////////////this prints to a local printer, but i can't get it to print a varriable instead of "No worries mate", can you help me with this?   I just found this way out, and it works perfectly!///////////////////////////////////


#include <iostream.h>
#include <stdio.h>

int main()
{
      FILE * f = fopen("lpt1","wt");

   if(f)
   {
         fprintf(f, "No Worries %s\n","Mate");
      fclose(f);
   }
   else puts ("failed");

   int delay;
   cin>>delay;
   return 0;
}
0
nietodCommented:
That only works if lpt1 has been rediected to the appropriate network printer.  (if the network printer "captures" lpt1.)   This tends to be a more problematic approach because the user, or even other programs may change the redirection.  You really should be able to get it to work by printing directly to the printer file.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.