Javascript required
Skip to content Skip to sidebar Skip to footer

What Is Needed to Send C Program Output Directly to a Printer

  1. 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 compile and run the programme using gcc, my printer low-cal flashes, but aught else happens.

    When I add the line:

    Lawmaking:

    fprintf(printer, "\f");
    and so my printer prints out a bare page.

    What am I doing wrong?
    Am grateful for any assistance, only please conduct in mind I am a beginner in C.


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

  3. 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?

  4. 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.


  5. 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.


  6. 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");
    If anyone else has any ideas and so I'd be grateful.
    Thank you in accelerate.

  7. Re: C - directing output to printer

    Quote Originally Posted by Tony Flury View Post

    I would imagine most "modern" printers do not expect just straight Ascii text, and that the drivers generate something altogether more than complex.

    I don't think you can send a text file for instance direct to "/dev/usb/lp0" - yous need to go via the printer commuter - i.e. lp.

    using lp sounds similar a good thought to me - equally y'all would and so us the preferences that the user has set - instead of your application over-riding them.

    That seems like it makes sense. I found various means to send output to the printer in C, ranging from 'stdprn', to 'PRN' to 'LPT1:' to '/dev/lp1'
    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");

  8. 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.

  9. 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.


  10. 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; }
    Notation "-P PDF" sets the printer. In my case I have installed "cups-pdf" to test information technology by printing to a pdf file (I have no printer here anyway).

    You can meet the list of printers with the "lpinfo" command.


howittworidence64.blogspot.com

Source: https://ubuntuforums.org/showthread.php?t=1301744