【问题标题】:Access Printer through C code通过 C 代码访问打印机
【发布时间】:2013-07-19 06:24:06
【问题描述】:

这是访问(外围驱动器)打印机的示例代码。但似乎stdprn 不起作用。我怎样才能让它活着?或者有没有其他合适的方法可以通过打印机打印任何东西。

/*print_it.c-This program preints a listing with line numbers */
#include <stdlib.h>
#include <stdio.h>
void do_heading(char *filename);
int line = 0, page = 0;
int main( int argv, char *argc[] )
{
    char buffer[256];
    FILE *fp;
    if( argv < 2 )
    {
        fprintf(stderr, "\nProper Usage is: ");
        fprintf(stderr, "\nprint_it filename.ext\n" );
        return(1);
    }
    if (( fp = fopen(argc[1], "r" )) == NULL )
    {
        fprintf( stderr, "Error opening file %s!", argc[1]);
        return(1);
    }
    page = 0;
    line = 1;
    do_heading( argc[1]);
    while( fgets( buffer, 256, fp ) != NULL )
    {
        if( line % 55 == 0 )
            do_heading(argc[1] );
        fprintf(stdprn, "%4:\t%s" , line++, buffer );
    }
    fprintf( stdprn, "\f" );
    fclose(fp);
    return 0;
}
void do_heading( char *filename )
{
    page++;
    if ( page > 1 )fprintf(stdprn, "f" );
    fprintf( stdprn, "Page: %d, %s\n\n", page, filename );
}
return 0;
}

谢谢。

【问题讨论】:

标签: c printing


【解决方案1】:

看起来stdprn 是 1980 年代某些 MS-DOS 编译器中存在的东西。它不是任何 C 标准的一部分,也没有在现代编译器中实现。

也许可以通过将其添加到您的程序中来自己定义它:

#ifdef _WIN32
#define PRINTER_DEVICE "PRN"
#else  /* assume unix */
#define PRINTER_DEVICE "/dev/lp0"
#endif

FILE *stdprn = fopen(PRINTER_DEVICE, "w");
/* should check for errors if stdprn == NULL */

【讨论】:

  • 如果这么落后那么有什么合适的方法可以通过打印机打印任何东西吗? @乔尼
  • 没有可移植的,你必须使用操作系统 API。如果打印机物理连接到机器(不是网络打印机),打开打印机设备并向其写入纯文本“可能”可以工作,但您将无法设置字体或打印图形。
  • "如果打印机物理连接到机器(不是网络打印机),打开打印机设备并向其写入纯文本“可能”工作”告诉我如何做到这一点。我只需要打印几行。
  • 没错。我认为可能还有其他解决方案。感谢您的回复。 :) @Joni
猜你喜欢
  • 1970-01-01
  • 2018-08-01
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 2014-06-27
相关资源
最近更新 更多