Thread: how do you print in C

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    how do you print in C

    I'm trying to learn how to print out reports in c with page breaks and such and I have a usb printer that the c language doesn't support I also have a old xp machine that has the old LPT1 printer port on the back if I install my compiler on that machine will it be able to access the port to where I can print using c

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    C doesn't know about printers, so there is nothing built in to C or C compilers that can magically talk to a printer. Furthermore, how you talk to a USB or parallel (e.g. LPT1) device depends on what OS you're using. The language you speak to the printer over that port, depends on the type of printer you have. I think most modern printers will speak PCL and possibly PS. You will have to find a library to help you output correct PCL or PS, or you will have to write your own code to do so.
    EDIT 2: I missed that you said Windows XP. The Microsoft run time provides a printer API you can use: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx. That should handle most of the PCL/PS stuff for you as well as the USB/parallel port stuff.

    Note, there may be better solutions to your problem of "printing a report", depending on how much flexibility you have with choosing a language, using 3rd party libraries and programs, etc. Basically, there's lots of stuff to help you print that is already out there. Why reinvent the wheel?

    EDIT: Giving us some more info on the nature of these reports, who they're for (personal, business internal or for business clients, etc), we may be able to give you more suggestions.
    Last edited by anduril462; 11-08-2013 at 11:58 AM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It might be easiest to write your text to a file and them call:
    Code:
    system("print yourfile.txt");
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    There student problems that solve simple business problems there console apps that is a report using arrays I can't post them because I haven't written them I did once and gave up because of this printing problem. So you say there are libraries that can help with this if there is how do I find them I could probably get help on how to implement them if I knew where to find them Oh thanks for giving me some insight into this I thought there was printer commands in c

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by artistunknown View Post
    So you say there are libraries that can help with this if there is how do I find them I could probably get help on how to implement them if I knew where to find them
    Google is one of the more common methods for finding printer libraries for windows xp, or whatever search terms are appropriate for your task. Note my "EDIT2" section in post #2, which links you to the built-in printer functions that Windows (including XP) should have available. These functions are in a library that Windows provides. Your compiler should know how to link with that.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I tried system print but it gave me this message

    unable to initialize device prn

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by artistunknown View Post
    I tried system print but it gave me this message
    unable to initialize device prn
    I don't have a usb printer so I'm not sure what to do or whether print will work with them or not.
    You could try to find it in the device manager and see if it has a device name of some sort. Then you'd do this:
    Code:
    system("print /D:device yourfile.txt");
    where "device" is the device name.

    Also, try printing something from notepad to see if you can get your printer to work at all.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    device do you mean the name of the printer I'm not sure what you mean by device I can print to notepad easily that's not a problem so i can print in windows anyway.
    Sorry to be such a pain if I could get system to work I'll use it if it does what I think

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Open notepad.
    Select print from the file menu.
    Right-click on the picture of your printer and select properties.
    Select the ports tab.
    Find your printer in the list.
    I believe that the string under the port column is the device name.
    But maybe not.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    There's no such thing like a picture of the printer in notepad are you talking about notepad++

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Ok I found it I understand what you mean now

  12. #12
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    It's implementation-dependent. Your system should provide a way to redirect your program's output to a printer.

    EDIT: On most Unix-like systems that's as easy as:
    ./program > /dev/lp

    You could also format your output specifically for a printer by passing the output to a program like pr:
    ./program | pr >/dev/lp
    Last edited by Barney McGrew; 11-08-2013 at 01:38 PM.
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I tried system device thing and I got to say in the message txt is currently being printed something like that but nothing would happen
    as for what Barney talking about I have no idea what that stuff is
    Maybe I should give up and not bug you guys about it anymore

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I have no information that can help you with your problem - just wanted to say that you shouldn't give up so easily. The long and painful lessons are often the ones that are most rewarding when you finally get it. I have encountered quite a few hurdles in programming that lasted hours, if not days, and were finally conquered through stubborn perseverance.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by artistunknown View Post
    I tried system device thing and I got to say in the message txt is currently being printed something like that but nothing would happen
    as for what Barney talking about I have no idea what that stuff is
    Maybe I should give up and not bug you guys about it anymore
    Barney's referring to a very simple system designed for eunuchs.
    Not the overly-complicated badly-designed system we are using.

    Anyway, if you just need to print your text file, you could do it from notepad.

    And you could of course try anduril's approach, but you'll still have to be able to refer to your printer somehow.

    What exactly was your system command?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print statement won't print
    By Sakari in forum C Programming
    Replies: 17
    Last Post: 06-20-2012, 06:41 PM
  2. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  3. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM
  5. print first and print last function
    By RawleyMacias in forum C Programming
    Replies: 6
    Last Post: 02-14-2002, 10:28 PM