How to catch errors when printing source code in C

I know how easy it is to print a text file on a printer: (See My question below code)

#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 due to an error when using this technology to print text to a printer. What if the user does not have a working or usable printer at that time? I want my program to spit out something like: "Error: the printer does not exist!".

What can I do about it? Thanks!

+4
source share
1 answer

You can check if the printer is on the network, but only if you have access to kernel mode, if you are a print driver or under Windows 95/98.

Typically, the printer port address is set to 0x378 (parallel port data register). Adding one ( 0x379 ) to this gives us the address of the parallel port status register. bit 4 of the Status Register (SELECT) indicates whether the printer is online or offline. if the bit is set, then the printer is connected to the network and if its 0, the bit is disabled. It might look like this:

 int status; // get status register value at 0x379 status = _inp (0x379); if (status & 0x10) // check bit 4 { // printer online } else { // printer offline } 

Here is another member of this register:

  bit 1 : DCN bit 3 : FAULT bit 4 : SELECT bit 5 : PAPER END bit 6 : ACKNOWLEDGE bit 7 : BUSY 

It comes from codeguru . But note that it is better to use a higher interface like api printer in WIN32 (OpenPrinter (), WritePrinter () StarDocPrinter (), StartPagePrinter (), etc.)

+2
source

Source: https://habr.com/ru/post/1398077/


All Articles