0

I am trying to print just specific string to a line printer. I try to run this snippet but nothing prints out. I am looking also at the list of pending jobs for the printer, and nothing shows when I run the code.

I can print documents just fine from Word, so the printer is available.

Can someone hint at what the problem may be?

#include <stdio.h>
#include <stdlib.h>
int main()
{
 FILE* printer = 0;
 if(( printer = fopen("lpt1", "a+")) == NULL)
 {
    puts("error opening printer");
 }
 char* text = "This is a test printing";

 if ( (fprintf(printer, "%s" , text) ) < 0  ){
     perror("Printing error");
 } 

 fflush(printer);
 fclose(printer);
 return 0;
}
Ann
  • 693
  • 3
  • 12
  • 17
  • 1
    It `lpt1` some sort of special file ? – cnicutar Sep 03 '13 at 19:39
  • No, it'sa parallel serial port. It shows on the device manager along with COM1 COM2, etc. – Ann Sep 03 '13 at 19:45
  • I think most people output to a file and then print it from Windows. But to do it in one step, you need to open a program for output rather than a file. That's explained here: http://stackoverflow.com/questions/450865/what-is-the-equivalent-to-posix-popen-in-the-win32-api – dcaswell Sep 03 '13 at 20:14
  • Assuming your text is actually reaching the printer, a page printer (which is what most modern printers are) won't print anything until the page is complete. Try adding a form feed character (`'\f'` or `'\x0c'`) to the end of your string. – arx Sep 03 '13 at 20:47
  • 1
    This might help: http://support.microsoft.com/kb/138594/en-us – alk Sep 04 '13 at 05:55

2 Answers2

1

I think you are misunderstanding that code. The code you submitted writes a string "This is a test printing" to a file in the same directory called "lpt1".

What you are probably wanting is to write out to something like "/dev/lpt1", and you should be able to test this by running

echo "this is my printed text" >/dev/lpt1
samsquanch
  • 521
  • 3
  • 12
0

No, you cannot write to a printer port using fopen() on Windows. The closest you can get is spawning cmd.exe and using the print command to print what you want. You can write what you want to a temp file first and then have print print it for you.

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47