What Is Needed to Send C Program Output Directly to a Printer
-
C - directing output to printer
Hi,
I'm reading a book virtually C and am stuck on a section regarding how to send text directly to the printer (as opposed to the screen).I have:
Lawmaking:
#include <stdio.h> principal() { FILE *printer = fopen("/dev/usb/lp0", "w"); fprintf(printer, "How-do-you-do World."); fclose(printer); return 0; }
When I add the line:
Lawmaking:
fprintf(printer, "\f");
What am I doing wrong?
Am grateful for any assistance, only please conduct in mind I am a beginner in C.
-
Re: C - directing output to printer
You may need to fflush() the output.
What results do you get with:
Code:
#include <stdio.h> primary() { FILE *printer = fopen("/dev/usb/lp0", "w"); fprintf(printer, "Howdy World.\due north\f"); fflush(printer); fclose(printer); return 0; }
-
Re: C - directing output to printer
Hi,
Cheers for the reply.
I compiled and ran the lawmaking you suggested and unfortunately the printer but spat out a blank page.
This seems to be caused by the "\f" character.
I likewise just tried printing something from the text editor to make sure that information technology is not the printer at fault and this worked fine
Any ideas?
-
Re: C - directing output to printer
I looked into an application I wrote over 11 years ago under Solaris, and I must've had the same problem yous are experiencing. In the lawmaking, I placed a annotate that sending information to the printer does non work as "gracefully" as ane would hope. My cheesy solution was to output the data to a file, and then using the system() library call, I was able to phone call "lp".
Sorry I cannot assistance more.
-
Re: C - directing output to printer
I would imagine virtually "modern" printers do not wait simply straight Ascii text, and that the drivers generate something altogether more complex.
I don't think you tin can send a text file for instance direct to "/dev/usb/lp0" - you need to go via the printer commuter - i.eastward. lp.
using lp sounds like a skillful idea to me - equally you would then the states the preferences that the user has set - instead of your application over-riding them.
-
Re: C - directing output to printer
Thanks for your answer..
I accept been playing around with this and googling all evening and am unfortunately still no closer to solving my problem.
What is irritating me is that I am obviously able to connect to the printer in some style equally it will chuck out a blank page when I send it:
Code:
fprintf(printer, "\f");
Thank you in accelerate.
-
Re: C - directing output to printer
Originally Posted by Tony Flury
Everything I tin can find online withal, points to this beingness rather dated.
Could yous give me a hint as to what the command would wait like to send output via the printer driver?
I tried this, without success:
Code:
FILE *printer = fopen("lp0", "west");
-
Re: C - directing output to printer
I would employ the idea of dumping your output to a text file then using the lp command to print it.
-
Re: C - directing output to printer
This kind of thing is fine for merely messing around, but a serious awarding that wants to impress should never even recall of talking to the printer directly similar this. Instead you should employ a printing API that abstracts away the actual printer device.
The simply fourth dimension I've printed from an application, I used wxWidgets which handled it all rather only.
-
Re: C - directing output to printer
The "\f" is a form feed, it ejects the folio and gets another.
Try this:
Lawmaking:
#include <stdio.h> int main(int argc, char **argv) { FILE *printer = popen("/usr/bin/lpr -P PDF", "west"); if (printer == Nil) render 1; printf("OK\northward"); fprintf(printer, "alsdjakljdlaskjd\northward"); pclose(printer); return 0; }
You can meet the list of printers with the "lpinfo" command.
howittworidence64.blogspot.com
Source: https://ubuntuforums.org/showthread.php?t=1301744