# 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. 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. You mean with sprintf() or something?
Code:
char command[BUFSIZ];
sprintf(command, "print \"&#37;s\\%s\"", "C:\\", "text.txt");
system(command);
Can you be more specific?

3. 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\\&#37;s.txt","w",filename);
Except I don't want the error messages. Any advice?

Thanks
-Matt

4. 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\\&#37;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. In case tabstop wasn't clear: only the printf() and scanf() family of functions uses format specifiers like &#37;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.

6. Originally Posted by mcotter222
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

7. 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("");

8. 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. Is that XP command 100&#37;, because everything worked, but my printer did nothing? Why?

10. Originally Posted by mcotter222
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. 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\"");
}

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

13. I edited the code. Change the LPT1 to the printer your port is in, then try it again.

14. Did it work for anyone? I editted it again, and this time it removes the file aswell.

15. 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 First 12345 Last