附加到文件 [英] Appending to a file

查看:65
本文介绍了附加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将数据附加到文件中。我希望每次运行脚本时都会从换行符发生这种情况。例如。

file1.txt包含


一个quickcfsgggdra ertge

Brown fox s; ofj; ekf; okfo

跳过sldkfhlrigje


现在我想追加来自file2.txt的以下数据


超过懒狗


现在file1.txt应该看起来像


一个快速的电子邮件

Brown fox s; ofj; ekf; okfo

跳过sldkfhlrigje

超过懒狗


请建议code!

Hi
I am appending the data to a file . I want this to happen from a newline everytime the script is run.For eg.
the file1.txt contains

A quickcfsgggdra ertge
Brown fox s;ofj;ekf;okfo
jumped sldkfhlrigje

Now I want to append the follwoing data from file2.txt

over the lazy dog

now file1.txt should look like

A quickcfsgggdra ertge
Brown fox s;ofj;ekf;okfo
jumped sldkfhlrigje
over the lazy dog

Please suggest the code!

推荐答案

http://perldoc.perl.org/functions/open.html


只需在追加模式下打开文件句柄,并确保打印的每一行以新行字符结尾。

http://perldoc.perl.org/functions/open.html

Simply open the file handle in append mode, and be sure that each line that you print ends in a new line character.

展开 | 选择 | Wrap | 行号


@ homesick123


应首选3-arg形式的open():
@homesick123

The 3-arg form of open() should be preferred:
展开 | 选择 | Wrap | 行号



应首选3-arg形式的open():
The 3-arg form of open() should be preferred:



为什么这是首选?


即使perdoc网站上的示例也不使用3参数形式:

Why is that preferred?

Not even the examples on the perdoc site use the 3 argument form:

展开 | 选择 | Wrap | 行号


这篇关于附加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆