简体   繁体   English

ofstream 可以用于在打印机上打印吗

[英]Can ofstream be used for printing on a printer

Can ofstream be used to write on a printer? ofstream可以用来在打印机上写字吗?

eg:例如:

string nameOfPrinter = "xyz";
ofstream onPrinter(nameOfPrinter);
onPrinter << "Printing.... ";

If I do as above will I get the output by the printer (on the paper) ?如果我按照上述操作,我会通过打印机(在纸上)得到 output 吗?

If not, why I won't get the output?如果没有,为什么我不会得到 output? Please suggest a way to print using a printer.请建议一种使用打印机打印的方法。

I am targeting the Windows platform (32bit)我的目标是 Windows 平台(32 位)

If you happen to have your printer associated with LPT1 and a printer which support formfeeds.如果您碰巧有与 LPT1 关联的打印机和支持换页的打印机。

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
    ofstream printer ("LPT1");
    if(!printer)
    {  return 1;
    }

    printer.puts("Test Test Test\n");
    printer.putc('\f');
    printer.close();
    return 0;
} 

LPT1 is also a file name in windows. LPT1 也是 windows 中的文件名。 But as known it is a reserved filename.但众所周知,它是一个保留的文件名。 So it will not be possible to have more than one file with the name LPT1.所以不可能有多个名为 LPT1 的文件。 And this file is already managed by windows.并且这个文件已经由 windows 管理。

See for reserved filenames查看保留的文件名

Yes you can, Your code should be:是的,你可以,你的代码应该是:

ofstream printer;
printer.open("lpt1");

I believe it's case sensitive(not sure "lpt1" or "LPT1").我相信它区分大小写(不确定“lpt1”或“LPT1”)。 Also, you'll need to write a page eject command.此外,您需要编写一个页面弹出命令。

EDIT:编辑:
LPT (Line Print Terminal) is name of the parallel port interface on IBM PC-compatible computers. LPT(行打印终端)是 IBM PC 兼容计算机上并行端口接口的名称。 Over the years, the parallel port interface has decreased use because of the rise of Universal Serial Bus.多年来,由于通用串行总线的兴起,并口接口的使用减少了。

In DOS , the parallel ports could be accessed directly on the command line.DOS中,可以直接在命令行上访问并行端口。 For example, the command type c:\autoexec.bat > LPT1 would direct the contents of the autoexec.bat file to the printer port(recognized by the reserved word LPT1"). A PRN device was also available as an alias for LPT1.例如,命令type c:\autoexec.bat > LPT1会将autoexec.bat文件的内容定向到打印机端口(由保留字 LPT1" 识别) PRN设备也可用作 LPT1 的别名。

Microsoft Windows still refers to the ports in this manner in many cases, though this is often fairly hidden. Microsoft Windows在许多情况下仍然以这种方式引用端口,尽管这通常是相当隐藏的。

In the Linux operating system the first LPT port is available via the filesystem as /dev/lp0 .Linux操作系统中,第一个LPT端口可通过文件系统作为/dev/lp0使用。

To write to a printer, one should simply open the printer as if it were a file (the printer name is system-dependent; on Windows machines, it will be lpt1 or prn , while on unix machines, it will be something like /dev/lp ), then write whatever text has to be written.要写入打印机,只需像打开文件一样打开打印机(打印机名称取决于系统;在 Windows 机器上,它将是lpt1prn ,而在 unix 机器上,它将类似于/dev/lp ),然后写下必须写的任何文本。

Sample program could be as simple as:示例程序可以很简单:

std::ofstream print; 
print.open("LPT1");
if (!print)
    return 0;
print << data;
print.close();

How would the file stream know the difference between the name of a printer and a file that just happened to share the name of a printer? stream文件如何知道打印机名称和刚刚共享打印机名称的文件之间的区别? So no;所以不行; you can't print to a printer by specifying the name of a printer.您不能通过指定打印机的名称来打印到打印机。

Printing in Win32 is not a trivial task.在 Win32 中打印并非易事。 You can't simply shove some characters at a printer;您不能简单地将一些字符推到打印机上。 it needs to know about page layout, fonts, etc. Basically, the way to do it from Win32 is to "draw" to the printer with GDI commands.它需要了解页面布局、fonts 等。基本上,从 Win32 执行此操作的方法是使用 GDI 命令“绘制”到打印机。 Beginner-level info can be found here .初级信息可以在这里找到


Correction: apparently, you can stream output to the printer with a stream.更正:显然,您可以使用 stream 将 stream output 连接到打印机。 However, it requires that the user has enabled some legacy functionality, so it isn't necessarily always available.但是,它要求用户启用了一些遗留功能,因此它不一定总是可用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM