ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I know how to fork and create child processes but what's it got to do with invoking a printing command? I can't believe that such a simple task is taking so much time.
I know how to fork and create child processes but what's it got to do with invoking a printing command? I can't believe that such a simple task is taking so much time.
/usr/bin/lpr and /usr/bin/lp are binary commands which you can run and you can run them via an exec() call. Since you know how to fork() and exec(), what's the problem?
You are within a C program, at least that's what I thought you started saying. Create an argument list, fork() a child, and then run exec() using either lpr or lp and passing it the argument list. This recommendation is exactly what you asked, which was:
Quote:
So how does one invoke the lpr command or lp command in a c program?
A: You use fork() and exec(). I offered "an" example, and suggested also that a general web search for examples may also give you some guidance.
And NevemTeve said the same recommendation back in post #9.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Completely untested (and ugly) code (I don't have a local printer on my Linux box)
Code:
#include <stdio.h>
int main(){
FILE *in = fopen("/home/user/Desktop/texts/abc.txt", "r");
FILE *pr = fopen("/dev/lp0", "w"); //Try /dev/usb/lp0 if you have USB printer
if(!in){printf("Failed to open input file\n"); return -1;}
if(!pr){printf("Failed to open printer\n"); return -1;}
while(!feof(in)){
fputc(fgetc(in), pr);
}
return 0;
}
i have a usb printer hp laser jet 1020 attached with my ubuntu desktop. Entering the command on the terminal ,lp filename gets the job done as far as the terminal is concerned. But the real challenge is to invoke command from a C program.
Apparently you don't and you're not even looking at links or any information you've been referred too.
Quote:
Originally Posted by goldriver92
i have a usb printer hp laser jet 1020 attached with my ubuntu desktop. Entering the command on the terminal ,lp filename gets the job done as far as the terminal is concerned. But the real challenge is to invoke command from a C program.
You've been told by two different people and you've claimed to know how to do the actions you were recommended to do. I'm suspecting you just don't want to invest the time.
Just so you don't "go off" although that's still likely.
What EXACT shell command do you issue to print that file as you're describing above? "lp filename" is one example.
And what follows is an incomplete example, but one which will work. Incomplete because you're just saying "I can't do it" as well as "This is so difficult"; so start with this and if you have questions, send them along. If you don't follow this and persist with saying this is so difficult, sorry but you're beyond any assistance level I can provide you at this time.
First i would like to thank you for taking the time out and answering my questions . I am sorry if it sounds like i don't want to invest time into this problem whereas i have spent 2 nerve wrecking days on multiple forums posting the problem and not getting a satisfactory answer from any place. I know forking as i have created child processes before to handle multiple clients wanting a service from a serve. In that sense, i know forking but i do not know how to use them with exec() command plus what is the objective of creating a child?
why can't i just use this?
system("lp /home/user/Desktop/xfolder/network.txt");
Someone told me that this system command will hang my code whereas my server is multi-threaded and it would probably have multiple clients using the print service which means it wants to go back to the listening state as soon as it sends out the system command.
1.Posting the printer thing again was to let smeezekitty know my exact problem.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Someone told me that this system command will hang my code whereas my server is multi-threaded and it would probably have multiple clients using the print service which means it wants to go back to the listening state as soon as it sends out the system command.
The system() command is quite inelegant but it can be used. It is a blocking call which means your code will
stop until the started process ends EXCEPT I believe you can add an & to the end to run it in the background.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.