在 C 中打印原始文本时如何捕获错误
我知道如何简单地将文本文件打印到打印机: (请参阅代码块下方的我的问题)
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查打印机是否在线,但仅如果您有权访问内核模式、如果您是打印驱动程序或在 Windows 95/98 下。
通常,打印机端口地址设置为
0x378
(并口数据寄存器)。加一 (0x379
) 即可得到并行端口状态寄存器的地址。状态寄存器(SELECT)的位 4 告诉我们打印机是在线还是离线。如果该位被设置,则打印机处于在线状态如果为0,则该位离线。
它看起来像这样:
这是该寄存器的其他成员:
它来自 代码大师。但请注意,您最好使用更高的接口,例如 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 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.)