Link to home
Start Free TrialLog in
Avatar of nate222
nate222

asked on

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.
Avatar of nietod
nietod

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?
Avatar of nate222

ASKER

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.
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?
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
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.
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.
Avatar of nate222

ASKER

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.
 
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?
Nate, did you try nietod's first answer?
              --------------------------
What mistake did you get?
Or did it just not work?

Moshe
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.
Avatar of nate222

ASKER

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.
Avatar of nate222

ASKER

It is borland c++
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nate222

ASKER

This is what I needed, but it still don't print to the network printer...any ideas why???
What exactly are you doing?  can you post the code?
Avatar of nate222

ASKER

#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!!!

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);
Avatar of nate222

ASKER

      ////////////////////////////////////////////
   //////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///////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;
}
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.)
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.
Avatar of nate222

ASKER

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
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.
Avatar of nate222

ASKER

////////////////////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;
}
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.