zoukankan      html  css  js  c++  java
  • Perl语言入门--3--文件读取与写入

    现有文件test.txt,内容为:"123 456"

    1,打开文本test.txt

          #!/usr/bin/perl

          open d,"test.txt";

        d 为文件句柄,指向打开的文件

    2,逐行读取文本test.txt

          #!/usr/bin/perl

          open d,"< test.txt";

          while(<d>){

              print $_;

           }

           close d;

          结果:123

                   456

         或者:print <d>;

         结果:同上

         <> 为取行操作符

    3,向test111.txt中写入内容

       #!/usr/bin/perl

        open d,">test111.txt";

        print f "hello,world see you ";

        close d;

        如果test111.txt原本有内容,则原内容将会被擦除,test111.txt的内容为

                        hello,world 

                        see you

    4,向test111.txt中追加内容

        #!/usr/bin/perl

          open d,">>test111.txt";

          print d "thank you ";

          close d;

          test111.txt的内容为 

                  hello,world

                  see you

                  thank you

         

       print 相当于写命令,别丢了语句结尾的 “;"

  • 相关阅读:
    表格边框的合并
    solr服务搭建
    JedisClient操作redis 单机版和集群版
    redis集群的搭建
    FastDfs的搭建
    打开与关闭端口
    zookeper分布式搭建1
    zookeper分布式搭建
    Zookeeper的安装
    Linux下Jdk的安装
  • 原文地址:https://www.cnblogs.com/modaidai/p/6690839.html
Copyright © 2011-2022 走看看