Thread: How to print directly from compile screen?

  1. #16
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Quote Originally Posted by matsp View Post
    If it's a console appliction (in Windows), you can open "lpt:" as a file [for writing]:
    Code:
      FILE *flpt;
      flpt = fopen("lpt:", "w");
      if (flpt == NULL) { 
         perror("open LPT:"); 
         exit(1); 
      }
    ...
    Then use "flpt" as your destination file.

    --
    Mats
    I tryed this, but exit was undefined. Also, what do you mean when you say "Then use "flpt" as your destination file"?

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    exit is defined in <stdlib.h>, so make sure you #include that header file.

    Also, what do you mean when you say "Then use "flpt" as your destination file"?
    matsp meant that after that code has executed, the FILE stream flpt has been opened, and you can write to it. For example:
    Code:
    fprintf(flpt, "This goes to the printer.\n");
    And, of course, everything printed to this file stream gets send to the printer. Theoretically.

    You could also try the non-standard stdprn, although I hear it doesn't exist under MSVC. http://www.thescripts.com/forum/thread218908.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Sorry to bring up old news guys, but I finally got a hold of a computer with a printer. I coded this, and nothing happened:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      FILE *flpt;
      flpt = fopen("lpt:", "w");
      if (flpt == NULL) { 
         perror("open LPT:"); 
         exit(1); 
      }
      fprintf(flpt, "Test print.\n\nTest print.\n\nTest print.\n\n");
    }
    Missing anything?

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, you may need to use "LPT1:" (or some other number after LPT and before .

    Also, some printers need special instructions to tell it to print things (e.g. a postscript printer will most likely not do anything meaningfull if you just send some text to it).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Okay, so
    Code:
    flpt = fopen("lpt1:", "w");
    fixes that problem, right? Now, what would I do to tell the printer to print?

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How is the printer connected to your machine?
    What printer is it?

    It's highly likely that a USB printer will not respond to "LPT1:", for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    It has to be functional on any printer. I test it with a USB printer, so what number do you suggest if not 1, or do I not even use a number?
    Thanks
    -Matt

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want it to work on any printer, don't do it yourself. Use the operating system. It will have drivers for every printer that works with the system. An easy way to do this would be to output to a file, and have the user print this file. I'm sure this could be arranged with a system() call if you wanted to do it automatically.

    Plus, I would find a program that printed something every time it was executed rather annoying.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    I need it to print by itself every time. If that isn't possible to do on more than one printer, I guess I could find out what kind.

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your best bet in that case is to use a command line utility, in my opinion. Perhaps one of these will help.
    http://www.google.ca/search?hl=en&q=...e+Search&meta=
    http://www.robvanderwoude.com/2kprintcontrol.html

    I found out that XP has a "print" command. http://www.ss64.com/nt/print.html
    It looks like it just uses LPT1, though.

    It might help to know what printer you have.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #26
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    So I have to scrap my whole thing?

  12. #27
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not necessarily. You could create a file at first, and then if you figure out how to print directly, implement that instead . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #28
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Ok, so, I am a complete beginner, to accessing other files. I barely understand how what I have works. I hate to do it, but I need to ask for help on that one.

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what DO YOU have at the moment?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #30
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      FILE *flpt;
      flpt = fopen("lpt:1", "w");
      if (flpt == NULL) { 
         perror("open LPT:"); 
         exit(1); 
      }
      fprintf(flpt, "Test print.\n\nTest print.\n\nTest print.\n\n");
    }
    And I know that system() might help, but I have no idea what that does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this user get screen to run under root?
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 06-28-2009, 09:31 AM
  2. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  4. how to print a file on the screen
    By tameeyore in forum C Programming
    Replies: 4
    Last Post: 09-18-2005, 12:00 PM
  5. Print Screen
    By /\/\ E /\/ @ in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-11-2002, 05:37 PM