How to print directly from compile screen?

This is a discussion on How to print directly from compile screen? within the C Programming forums, part of the General Programming Boards category; I tryed that too, but it didn't work at all. I can get around that, though, if I can change ...

  1. #46
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    I tryed that too, but it didn't work at all. I can get around that, though, if I can change the name of the .txt file according to a string. How would I do that?

    -Matt

  2. #47
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You mean with sprintf() or something?
    Code:
    char command[BUFSIZ];
    sprintf(command, "print \"%s\\%s\"", "C:\\", "text.txt");
    system(command);
    Can you be more specific?
    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. #48
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    More specific:
    So, to open the file for what is stored in char filename, I would do:
    Code:
    char filename[4] = 'text';
    flpt = fopen("C:\\Documents and Settings\\USERNAME\\Desktop\\%s.txt","w",filename);
    Except I don't want the error messages. Any advice?

    Thanks
    -Matt

  4. #49
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    filename in your example needs to be
    Code:
    char filename[5] = "text";
    or better yet, leave the size out. And it appears that you do indeed want sprintf, as so:
    Code:
    char real_filename[128]; /* some large enough number */
    sprintf(real_filename, "C:\\Documents and Settings\\USERNAME\\Desktop\\%s.txt", filename);
    and then you can open real_filename.



    As an aside, I think I'll try to use "USERNAME" the next time I register for something....

  5. #50
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    In case tabstop wasn't clear: only the printf() and scanf() family of functions uses format specifiers like %s and %d. All other standard C functions generally just take single strings. If you need to use strings with format specifiers with other functions, you can use sprintf() first to create a single string from your printf-style arguments.
    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.

  6. #51
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mcotter222 View Post
    All I did was add this line to the end
    Code:
    remove("C:\\Documents and Settings\\USERNAME\\Desktop\\Test.txt");
    and nothing happened when I compiled it besides making the file Test.txt.
    It should definitely work - remove does return an error code, so perhaps something like this:
    Code:
    if (remove(...) != 0)
    {
        perror("Remove(...):");  // This will show an error such as "file not found" or such things
        exit(1);   // May not be the right thing.
    }
    Also, bear in mind the above comments on filenames and 8.3 format if you are using for example Turbo C to make a DOS executable (although this should apply to when you are creating the file too).

    --
    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. #52
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    As far as I'm aware, perror() automatically adds a colon to the message.
    Code:
    $ cat ./perror.c
    #include <stdio.h>
    
    int main() {
        perror("");
        perror("function");
        perror("function:");
        return 0;
    }
    $ ./perror
    Success
    function: Illegal seek
    function:: Illegal seek
    $
    (I guess perror() must also set errno to whatever corresponds to illegal seeks on my system, too.)

    So the best way to use perror is something like this:
    Code:
    perror("remove");
    perror("remove()");
    perror(filename);
    fprintf(stderr, "remove(): &#37;s: ", filename), perror("");
    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.

  8. #53
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    New problem. Everything worked, except when I got to the compile screen, it said it was printing, but it didn't. Any ideas as to why not?

  9. #54
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Is that XP command 100&#37;, because everything worked, but my printer did nothing? Why?

  10. #55
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mcotter222 View Post
    Is that XP command 100%, because everything worked, but my printer did nothing? Why?
    What command are you using? If it's a system() call, does the command work if you type it in yourself with a known good text-file; and if so, then are you using fclose() on the file before you try to print it?

  11. #56
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Okay, I tried fclose, but that didn't hep. Here's what I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      FILE *flpt;
      flpt = fopen("C:\\Documents and Settings\\alpha\\Desktop\\Test.txt", "w");
      fprintf(flpt, "Test print.\n\nTest print.\n\nTest print.\n\n");
      fclose(flpt);
      system("PRINT /D:LPT1 \"C:\\Documents and Settings\\alpha\\Desktop\\Test.txt\"");
    }
    Last edited by mcotter222; 04-21-2008 at 08:41 PM. Reason: Updated the code.

  12. #57
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure this only works for "DOS compatible printers" - I just tried on my machine, and it queued the print job, but it's not printing to my default printer, but the one on LPT1:, which is no longer available on my machine.

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

  13. #58
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    I edited the code. Change the LPT1 to the printer your port is in, then try it again.

  14. #59
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Did it work for anyone? I editted it again, and this time it removes the file aswell.

  15. #60
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Sorry, here is the newest code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      FILE *flpt;
      flpt = fopen("C:\\Documents and Settings\\alpha\\Desktop\\Test.txt", "w");
      fprintf(flpt, "Test print.\n\nTest print.\n\nTest print.\n\n");
      fclose(flpt);
      system("PRINT /D:LPT1 \"C:\\Documents and Settings\\alpha\\Desktop\\Test.txt\"");
      remove("C:\\Documents and Settings\\alpha\\Desktop\\Test.txt");
    }

Page 4 of 5 FirstFirst 12345 LastLast
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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21