如何“禁用”文件输出流
我正在处理一些遗留代码,其中有很多 WriteLn(F, '...') 命令分散在各处。这些命令中有一些有用的信息(变量包含哪些信息等),所以我不想删除它或注释掉它,但我想阻止程序写入文件。
有什么方法可以分配 F 变量,以便忽略写入其中的任何内容吗?我们使用控制台输出,因此这不是一个选项。
I'm working on some legacy code in which there are a lot of WriteLn(F, '...') commands scattered pretty much all over the place. There is some useful information in these commands (what information variables contain, etc), so I'd prefer not to delete it or comment it out, but I want to stop the program from writing the file.
Is there any way that I can assign the F variable so that anything written to it is ignored? We use the console output, so that's not an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回到很久以前 DOS 的美好时光 - 如果将“f”分配给设备“nul”,那么应该没有输出。
我不知道这在 Windows 中是否仍然有效。
编辑:
您还可以将“f”分配给文件 - 例如,assignfile (f, 'c:\1.txt')。
Going back a long long time to the good old days of DOS - If you assign 'f' to the device 'nul', then there should be no output.
I don't know whether this still works in Windows.
Edit:
You could also assign 'f' to a file - assignfile (f, 'c:\1.txt') - for example.
打开空设备并让输出到那里可能会起作用。在DOS下,NUL设备的性能差得惊人IIRC(据我了解,它没有缓冲,所以系统在处理每个字节时必须在设备表中查找NUL),但我一点也不感到惊讶如果它在新系统下得到改进。无论如何,这可能是您可以做的最简单的事情,除非您确实需要最大限度地提高性能。如果性能至关重要,理论上可能可以覆盖 WriteLn 函数,因此它对某些文件不执行任何操作,但不幸的是,我相信它允许任何用户定义函数不允许的语法形式。
否则,我建议执行正则表达式查找/替换,以可以机械恢复的方式注释掉 WriteLn 语句。
Opening the null device and letting output go there would probably work. Under DOS, the performance of the NUL device was astonishingly bad IIRC (from what I understand, it wasn't buffered, so the system had to look up NUL in the device table when processing each byte) but I would not be at all surprised if it's improved under newer systems. In any case, that's probably the easiest thing you can do unless you really need to maximize performance. If performance is critical, it might in theory be possible to override the WriteLn function so it does nothing for certain files, but unfortunately I believe it allows syntax forms that were not permissible for any user-defined functions.
Otherwise, I would suggest doing a regular-expression find/replace to comment out the WriteLn statements in a fashion that can be mechanically restored.