I know how to simply print a text file to the printer:
(See my question below the code block)
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
FILE * Printer = fopen("LPT1", "w");
FILE * FilePointer;
char str[256];
char buf[BUFSIZ];
FilePointer = fopen("sample.txt", "r");
if( !FilePointer )
{
printf("File does not exist\n");
return -1;
}
while( fgets ( buf, sizeof buf, FilePointer ) != NULL )
{
fprintf(Printer, "%s", buf);
}
printf("\nPrinting..\n");
fprintf(Printer, "\f");
getch();
return 0;
}
But my problem is for error catching when using this technique to print a text to printer.
What if the user has no valid or usable printer at that time? I want my program to spit out something like: “Error: printer does not exist!”.
Is there anything I could do with that? Thanks!
You can check if the printer is online, but only if you have access to kernel mode, if you are a printing driver or under Windows 95/98.
Usually, the printer port address is set
0x378(data register of parallel port). Adding one (0x379) to this gives us the address of the status register of the parallel port. bit 4 of the status register (SELECT) tells us whether the printer is online or offline. if the bit is set, then the printer is onlineand if its 0, the bit is offline.
It can look like this :
Here are the other member of this register :
It’s coming from codeguru. But take note you should better use a higher interface like the printer api in WIN32 (OpenPrinter(), WritePrinter() StarDocPrinter(), StartPagePrinter(), etc.)