Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1148697
  • 博文数量: 312
  • 博客积分: 12522
  • 博客等级: 上将
  • 技术积分: 3376
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-27 18:35
文章分类

全部博文(312)

文章存档

2016年(3)

2015年(1)

2013年(1)

2012年(28)

2011年(101)

2010年(72)

2009年(13)

2008年(93)

分类: IT业界

2011-07-13 23:09:13

打开文件

if (open(TXT,"C:/Documents and Settings/Administrator/桌面/integrate.txt")) {

    print "successed!";

}

else {

      print "failed.";

      exit 1;

}

 

关闭文件句柄

close(TXT); # TXT是句柄名

 

$!返回操作系统的相应出错消息

if (!open(TXT,"C:/Documents and Settings/Administrator/桌面/integrat.txt")){

    warn "cannot read integtate: $!";

} else {

    print " OK!";

    exit 1;

}

$!为“No such file or directory

 

读取文件

open(TXT,"C:/Documents and Settings/Administrator/桌面/integrate.txt") or die "cannot open integrate: $!";

$line = ;

print $line;

输出为文件的第一行

 

打印整个文件

open(TXT,"C:/Documents and Settings/Administrator/桌面/integrate.txt") or die "cannot open integrate: $!";

while (defined($a = )){

       print $a;

}

或者使用$_

while (){

        print $_;

}

w h i l e循环将负责把输入行赋予$_

或者使用

@contents=; #文件的每一行作为数组的一个元素

print @contents;  #打印整个文件

print $contents[1]; #打印文件的第二行

 

写入文件

open(TXT,">>C:/Documents and Settings/Administrator/桌面/test.txt") or die "cannot open integrate: $!";

if (! print TXT "This entry was written at ",scalar(localtime),"\n"){          

    warn "Unable to write the test file: $!";          # print完成追加写入

}else {

 print "Operation successde!";

}

close(TXT);

open(TXT,"C:/Documents and Settings/Administrator/桌面/test.txt") or die "cannot open integrate: $!";

@new=;

print @new;

close(TXT);

 

拷贝文件

open(T1,">>C:/Documents and Settings/Administrator/桌面/test.txt") or die "cannot open integrate: $!";

open(T2,"C:/Documents and Settings/Administrator/桌面/test2.txt") or die "cannot open integrate: $!";

@contents=;

$a=2011;

print T1 "@contents $a";  # test将被追加写入test2的内容和2011.

close(T1);

close(T2);

或者print T1 ; #效果相同,更为简洁


阅读(766) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~