ofstream可以用于在打印机上打印吗
可以使用ofstream
在打印机上写入吗?
例如:
string nameOfPrinter = "xyz";
ofstream onPrinter(nameOfPrinter);
onPrinter << "Printing.... ";
如果我按照上述操作,我会得到打印机的输出(在纸上)吗?
如果没有,为什么我得不到输出?请建议一种使用打印机进行打印的方法。
我的目标是 Windows 平台(32 位)
Can ofstream
be used to write on a printer?
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) ?
If not, why I won't get the output? Please suggest a way to print using a printer.
I am targeting the Windows platform (32bit)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您碰巧拥有与 LPT1 关联的打印机以及支持换页的打印机。
LPT1也是windows中的文件名。但众所周知,它是一个保留的文件名。因此,不可能有多个名为 LPT1 的文件。而且这个文件已经被windows管理了。
请参阅保留文件名
If you happen to have your printer associated with LPT1 and a printer which support formfeeds.
LPT1 is also a file name in 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. And this file is already managed by windows.
See for reserved filenames
是的,你可以,你的代码应该是:
我相信它区分大小写(不确定“lpt1”或“LPT1”)。此外,您还需要编写页面弹出命令。
编辑:
LPT(行式打印终端)是 IBM PC 兼容计算机上的并行端口接口的名称。多年来,由于通用串行总线的兴起,并行端口接口的使用已经减少。
在 DOS 中,可以直接通过命令行访问并行端口。例如,命令
type c:\autoexec.bat > LPT1
会将autoexec.bat
文件的内容定向到打印机端口(由保留字 LPT1" 识别)。PRN
设备也可用作 在许多情况下,Microsoft Windows
的别名仍然以这种方式引用端口,尽管
在
Linux
操作系统中,第一个经常被隐藏起来。 LPT
端口可通过文件系统为/dev/lp0
要写入打印机,只需打开打印机,就好像它是一个文件一样(打印机名称取决于系统;在 Windows 上)。机器上,它将是
lpt1
或prn
,而在 unix 机器上,它将类似于/dev/lp
),然后编写任何文本必须编写示例程序,如下所示:
Yes you can, Your code should be:
I believe it's case sensitive(not sure "lpt1" or "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. 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. For example, the commandtype c:\autoexec.bat > LPT1
would direct the contents of theautoexec.bat
file to the printer port(recognized by the reserved word LPT1"). APRN
device was also available as an alias for LPT1.Microsoft Windows
still refers to the ports in this manner in many cases, though this is often fairly hidden.In the
Linux
operating system the firstLPT
port is available via the filesystem as/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
orprn
, while on unix machines, it will be something like/dev/lp
), then write whatever text has to be written.Sample program could be as simple as:
文件流如何知道打印机名称和恰好共享打印机名称的文件之间的区别?所以不;您无法通过指定打印机名称来打印到打印机。
在 Win32 中打印并不是一项简单的任务。你不能简单地把一些字符塞到打印机上;它需要了解页面布局、字体等。基本上,从 Win32 执行此操作的方法是使用 GDI 命令“绘制”到打印机。初学者级别的信息可以在这里找到。
更正:显然,您可以使用流将输出流式传输到打印机。但是,它要求用户启用一些旧功能,因此它不一定始终可用。
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? So no; you can't print to a printer by specifying the name of a printer.
Printing in Win32 is not a trivial task. 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. Beginner-level info can be found here.
Correction: apparently, you can stream output to the printer with a stream. However, it requires that the user has enabled some legacy functionality, so it isn't necessarily always available.