LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1803|回复: 6

打开一个文件并加写入些东西。

[复制链接]
发表于 2003-11-26 14:18:43 | 显示全部楼层 |阅读模式
#!/usr/bin/perl -w
use strict;  #使用规范的格式检查脚本。
$a=9;
open (FILE,">>tmp.txt") or die "could not open tes.txt!";  #打开文件,>>的作用是附加。
while ($a>3 ){            #写操作。
print FILE "well\n";
$a--;
}
close FILE ;  #关闭文件句柄,关闭文件。
 楼主| 发表于 2003-11-26 21:02:14 | 显示全部楼层
也可以调用IO::File达到同样的功能。

R# cat print.pl
#!/usr/bin/perl -w
use IO::File;  # 调用IO::File
my $fh=IO::File->new(">log.txt");   #用IO::File-new()的方法打开一个文件,返回一个文件句柄。
print $fh "firest\n";
print $fh "second\n";  #这两行是写操作。
close $fh;   #关闭句柄。
发表于 2003-11-26 21:08:50 | 显示全部楼层
devel 有沒有興趣當版主?
要是有我第一個?硗赌阋黄 ;)

打印出文件的行数,由georgek提供。

#!/bin/perl -w
my $counter=0;
open(my $fh,'test');
while (my $line=<$fh>){$counter++;}
print "counted $counter line\n";
 楼主| 发表于 2003-11-26 21:26:57 | 显示全部楼层
最初由 georgek 发表
devel 有沒有興趣當版主?
要是有我第一個?硗赌阋黄 ;)




:help  :help :beat :beat :beat :beat  :comp :comp :comp
发表于 2003-11-26 21:35:00 | 显示全部楼层
hoho, 想?響摕o人反對
 楼主| 发表于 2003-11-26 21:39:21 | 显示全部楼层
换了系统,就不知道你打了什么字了:ask:ask看不出来。
 楼主| 发表于 2003-11-27 19:20:34 | 显示全部楼层
最初由devel发表
hoho, 想?響摕o人反對


原来georgek 想当版主很久了。你要当版主就投你一票
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表