[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: printing



> 
> Question for the C crowd: I have a program which I wish to print an ordered
> text file. In the Linux Programmers Guide, the printer chapter shows how to
> directly talk to the printer port. I'd like to be able to send the text to the
> appropriate printer filter (and spooler). How would one go about doing that?
> 

My quick and dirty attempt (I checked that it does compile though)
would be:

------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  FILE *fp;
  char* printername = "bla";
  char command[1024];

  sprintf(command, "lpr -P%s", printername);
  fp = popen(command, "w");
  if(fp == NULL) {
    fprintf(stderr, "Could not output to printer %s\n", printername);
    perror(NULL);
    exit(1);
  }

  fprintf(fp, "This goes to the filter known to /etc/printcap as %s\n",
            printername);
  pclose(fp);
}
------------------------------------------------------------------------

The interesting thing is in the popen()/pclose() functions.  You might
want to check the man page on those.  Problem is you cannot read error
messages from the lpr command in your program.  If you cannot live with
that, it will get much more complicated (interesting reading material
about communication betweeen processes is in the comp.unix.programmer
faq).

Eric Meijer

-- 
 E.L. Meijer (tgakem@chem.tue.nl)          | tel. office +31 40 2472189
 Eindhoven Univ. of Technology             | tel. lab.   +31 40 2475032
 Lab. for Catalysis and Inorg. Chem. (TAK) | tel. fax    +31 40 2455054


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
debian-user-request@lists.debian.org . 
Trouble?  e-mail to templin@bucknell.edu .


Reply to: