'>>' 之间的区别和'>'在 Perl 中

发布于 2024-09-04 11:00:40 字数 156 浏览 1 评论 0原文

这两个代码片段有什么区别?

  1. open(MYFILE, '>>data.txt');

  2. open(MYFILE, '>data.txt');

What is the difference between these two code snippets?

  1. open (MYFILE, '>>data.txt');

  2. open (MYFILE, '>data.txt');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

锦欢 2024-09-11 11:00:40
  1. open (MYFILE, '>>data.txt') — 打开data.txt,保留原始数据,追加来自结尾。
  2. open (MYFILE, '>data.txt') — 打开data.txt,删除里面的所有内容,并从头开始写入数据。

来自 perldoc -f open

如果 MODE 为 '<' 或什么都不是,则打开文件进行输入。如果 MODE 为 '>',则文件将被截断并打开以进行输出,并在必要时创建文件。如果 MODE 为 '>>',则打开文件进行追加,并在必要时再次创建。

它源于 shell 的用法,

  • cmd <; file.txt 将文件复制到标准输入,
  • cmd > file.txt 将 stdout 写入文件,
  • cmd>> file.txt 将 stdout 附加到文件末尾。
  1. open (MYFILE, '>>data.txt') — Open data.txt, keep the original data, append data from the end.
  2. open (MYFILE, '>data.txt') — Open data.txt, delete everything inside, and write data from the start.

From perldoc -f open:

If MODE is '<' or nothing, the file is opened for input. If MODE is '>', the file is truncated and opened for output, being created if necessary. If MODE is '>>', the file is opened for appending, again being created if necessary.

It stems from the shell usage that,

  • cmd < file.txt to copy file into stdin,
  • cmd > file.txt to write stdout into a file, and
  • cmd >> file.txt to append stdout to the end of the file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文