在 C 中打印原始文本时如何捕获错误

发布于 2025-01-08 17:09:33 字数 715 浏览 0 评论 0原文

我知道如何简单地将文本文件打印到打印机: (请参阅代码块下方的我的问题)

#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;
}

但是我的问题是使用此技术将文本打印到打印机时捕获错误。 如果用户当时没有有效或可用的打印机怎么办?我希望我的程序输出类似以下内容:“错误:打印机不存在!”。

我能做点什么吗?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断爱 2025-01-15 17:09:33

您可以检查打印机是否在线,但如果您有权访问内核模式、如果您是打印驱动程序或在 Windows 95/98 下。

通常,打印机端口地址设置为0x378(并口数据寄存器)。加一 (0x379) 即可得到并行端口状态寄存器的地址。状态寄存器(SELECT)的位 4 告诉我们打印机是在线还是离线。如果该位被设置,则打印机处于在线状态
如果为0,则该位离线。
它看起来像这样:

int status;

// get status register value at 0x379
status = _inp (0x379);

if (status & 0x10) // check bit 4
{
// printer online
}
else
{
// printer offline
} 

这是该寄存器的其他成员:

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

它来自 代码大师。但请注意,您最好使用更高的接口,例如 WIN32 中的打印机 api(OpenPrinter()、WritePrinter() StarDocPrinter()、StartPagePrinter() 等)

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 online
and if its 0, the bit is offline.
It can 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 are the other member of this register :

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文