加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl:在第N位写入文件

发布时间:2020-12-15 21:46:32 所属栏目:大数据 来源:网络整理
导读:我正在尝试写入Nth POSITION的档案.我试过下面的例子,但最后写了.请帮助实现这一目标. #!/usr/bin/perlopen(FILE,"+try.txt")ordie ("Cant open file try.txt");$POS=5; seek(FILE,$POS,0); print FILE "CP1"; 解决方法 您正在以读写附加模式打开文件.尝试以
我正在尝试写入Nth POSITION的档案.我试过下面的例子,但最后写了.请帮助实现这一目标.
#!/usr/bin/perl

open(FILE,"+>>try.txt")
or
die ("Cant open file try.txt");

$POS=5;

   seek(FILE,$POS,0);

   print FILE "CP1";

解决方法

您正在以读写附加模式打开文件.尝试以读写模式打开文件:
my $file = "try.txt";
open my $fh,"+<",$file
    or die "could not open $file: $!";

另外,请注意使用三个参数open,词法文件句柄和$!.

#!/usr/bin/perl

use strict;
use warnings;

#create an in-memory file
my $fakefile = "1234567890n";
open my $fh,$fakefile
    or die "Cant open file: $!";

my $offset = 5;

seek $fh,$offset,0
    or die "could not seek: $!";

print $fh "CP1";

print $fakefile;

上面的代码打印:

12345CP190

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读