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

发布于 2024-11-18 04:17:24 字数 326 浏览 2 评论 0原文

可以使用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 技术交流群。

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

发布评论

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

评论(3

你的他你的她 2024-11-25 04:17:24

如果您碰巧拥有与 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也是windows中的文件名。但众所周知,它是一个保留的文件名。因此,不可能有多个名为 LPT1 的文件。而且这个文件已经被windows管理了。

请参阅保留文件名

If you happen to have your printer associated with LPT1 and a printer which support formfeeds.

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

捶死心动 2024-11-25 04:17:24

是的,你可以,你的代码应该是:

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

我相信它区分大小写(不确定“lpt1”或“LPT1”)。此外,您还需要编写页面弹出命令。

编辑:
LPT(行式打印终端)是 IBM PC 兼容计算机上的并行端口接口的名称。多年来,由于通用串行总线的兴起,并行端口接口的使用已经减少。

在 DOS 中,可以直接通过命令行访问并行端口。例如,命令 type c:\autoexec.bat > LPT1 会将 autoexec.bat 文件的内容定向到打印机端口(由保留字 LPT1" 识别)。PRN 设备也可用作 在许多情况下,Microsoft Windows的别名

仍然以这种方式引用端口,尽管

Linux 操作系统中,第一个 经常被隐藏起来。 LPT 端口可通过文件系统为 /dev/lp0

要写入打印机,只需打开打印机,就好像它是一个文件一样(打印机名称取决于系统;在 Windows 上)。机器上,它将是 lpt1prn,而在 unix 机器上,它将类似于 /dev/lp),然后编写任何文本必须编写

示例程序,如下所示:

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

Yes you can, Your code should be:

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

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

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 first LPT 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 or prn, 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:

std::ofstream print; 
print.open("LPT1");
if (!print)
    return 0;
print << data;
print.close();
姜生凉生 2024-11-25 04:17:24

文件流如何知道打印机名称和恰好共享打印机名称的文件之间的区别?所以不;您无法通过指定打印机名称来打印到打印机。

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

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